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"

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.

Thats where your wrong because it works perfect

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.