I have this expression here
$data = file_get_contents('groups.txt');
function links1($l1, $l2){echo "<a href='". $l1 ."'><img src='". $l2 ."'/></a>";}
$parsed1 = get_string_between($data, "[Group1]", "[/Group1]");
$data = explode("\n", $parsed1);
foreach($data as $string2){
list($l1, $l2) = explode(' ', $string2);
links1($l1, $l2);}}
I have a get_string_between defined as a function above that gets anything between "[Group1]" and "[/Group1]". The text file groups contains stuff:
[Group1]
aaa 1.png
bbb 2.jpg
[/Group1]
The problem is that when I run this it outputs as
<a href=''><img src=''/></a><a href='aaa'><img src='1.png'/></a><a href='bbb'><img src='2.jpg'/></a><a href=''><img src=''/></a>
so I get these dummy two spaces i guess that are added to th开发者_StackOverflow中文版e front and back of anything thats between
aaa 1.png bbb 2.jpg
How can I fix that?
Basicly gow can i remove the <a href=''><img src=''/>
from start and back from the output?
Thanks!
edit: added the get_string_between function
function get_string_between(){
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
There are several problems with your get_string_between
function. I've tried to clean it up here:
function get_string_between($text, $start, $end) {
$n1 = strpos($text, $start);
if ($n1 === false) return '';
$n1 += strlen($start);
$n2 = strpos($text, $end, $n1);
if ($n2 === false) return substr($text, $n1);
return substr($text, $n1, $n2 - $n1);
}
Hopefully this will help/lead you in the right direction.
精彩评论