Page 1 of 1

detect first html tag

Posted: Thu Nov 25, 2010 7:30 am
by pedroz
need to find the position of the first html tag without </ after the word togowentgone

$a = '<p>hi</p><p>togowentgone</p> <br />'
here it would be <br />
// position=30

$b = '<br /><strong><p>togowentgone</p></strong> <hr />'
here it would be <hr />
// position=44

could you please give me a clue?

Re: detect first html tag

Posted: Thu Nov 25, 2010 12:02 pm
by Celauran

Code: Select all

$string = "<p><strong>togowentgone</strong></p> <hr />";
$needle = "togowentgone";

$start = stripos($string, '<', stripos($string, $needle));
while (substr($string, $start + 1, 1) == '/')
{
    $start = stripos($string, '<', $start + 1);
}
echo $start;
A little brute force, but it gets the job done. I'm sure someone will be along with a more elegant regex solution.