Welcome to WebmasterWorld Guest from 54.196.233.239

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

regex to parse html code

parse html code by regex

     

nasser bahaj

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

5+ Year Member



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 .

coopster

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

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



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].
 

Featured Threads

Hot Threads This Week

Hot Threads This Month