homepage Welcome to WebmasterWorld Guest from 54.145.209.80
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
regex to parse html code
parse html code by regex
nasser bahaj



 
Msg#: 4131906 posted 8:59 am on May 13, 2010 (gmt 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 .

 

coopster

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



 
Msg#: 4131906 posted 9:25 pm on May 19, 2010 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved