0

Thanks to a little guidance here I have written a function for getting part of a string returned.

function getcontent($tag,$string)
{
$pos1 = stripos($string, '<'.$tag.'>');
$pos2 = stripos($string, '<'.$tag.'>');
$content = substr($string, $pos1, $pos2);
return $content;
}
print $content('row','Hello world <row> bladh </row>');

output would be "bladh"

2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by blocblue
0

Sorry, but no it won't. It will return a blank string, because you're missing the closing slash from your tag on line 4.
R.

0

I beg to differ. It is conincidence that your example works. $pos1 returns 12, $pos2 returns 12, both the same index because you're missing the closing slash on the second tag match. Because the string isn't 24 characters long, you're getting the desired result.

Increase the length of text between the <row> tags, and you'll see that it no longer works. Also, you called the function incorrectly, but I guess this is a typo. It should be:

print getcontent('row','Hello world <row> bladh </row>');

not:

print $content('row','Hello world <row> bladh </row>');

And the correct function code should be:

function getcontent($tag,$string) {
    $pos1 = stripos($string, '<'.$tag.'>');
    $pos2 = stripos($string, '</'.$tag.'>');    // See the </
    $content = substr($string, $pos1, $pos2);
    return $content;
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.