detect first html tag

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

detect first html tag

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: detect first html tag

Post 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.
Post Reply