Welcome to WebmasterWorld Guest from 54.159.103.80

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

regex to parse html code

parse html code by regex

     
8:59 am on May 13, 2010 (gmt 0)

New User

5+ Year Member

joined:Apr 8, 2010
posts: 9
votes: 0


hi all

I am developing a code can put links at some words .

the code look like this :


$html='
<b> download firefox program now from this link <a href="http://www.download8.com/firefox">download firefox program now</a><b><br>';
echo $html;
$words2=array("firefox","program");
$links2=array("http://www.firefox.com","http://www.soft.com");
for ($i=0;$i<count($words2);$i++){
$words[]="/ ".$words2[$i]." /i";
$links[]=" <a href='$links2[$i]' style='color:red;'>$words2[$i]</a> ";
}
$count=5;
$html=preg_replace($words,$links,$html,$count);
echo $html;


now ,

this code is working well but if one of words ("firefox","program") already inside link this will damage the original link because it put link inside link .

for example this html code



$html='
<b> download firefox program now from this link <a href="http://www.download8.com/firefox">download firefox program now</a><b><br>';


I want to put this url "http://www.firefox.com" on "firefox" word but I want first to check if this word are already link (already inside <a></a> tags) , to avoid damage of existing links in the code .

I hope your help please

thank you .
9:25 pm on May 19, 2010 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12547
votes: 2


You could use lookahead or lookbehind assertions in your regular expression to check for anchor tags before or after the matching text. Or maybe even parse the document first using the DOM functions [php.net] or perhaps Tidy functions [php.net].
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members