drisate Posted March 31, 2011 Share Posted March 31, 2011 Hey guys i need to be able to find and replace some key words by links in a texte. I made a function that works great but i am having issues when the keyword found is already part of a link ... <?php function url_adder($string){ $select = mysql_query("SELECT * FROM rep_membre") or die(mysql_error()); while ($page = mysql_fetch_array($select)) { $ba = @current(@mysql_fetch_assoc(@mysql_query("SELECT batiment FROM numc where numc='$page[numc]'"))); $string = str_ireplace(html_entity_decode("$page[entreprise]"), '<A class="link2" href="index.php?mod=rep&bcode='.$page[numc].'&icode='.$page[id].'&sorter=carte&anchor='.$page[id].'">'.html_entity_decode($page[entreprise]).'</a>', html_entity_decode("$string")); } return $string; } ?> So let say that the key word is google and that the code code looks like this: I really love <a href="http://www.google.com">Google.com</a>! So once passeg inside the function it would look like this I really love <a href="http://www.<A class="link2" href="index.php?mod=rep&bcode=4&icode=5&sorter=carte&anchor=6">google</a>.com"><A class="link2" href="index.php?mod=rep&bcode=4&icode=5&sorter=carte&anchor=6">Google</a>.com</a>! It's obvious that this causes coding problems hehe Quote Link to comment Share on other sites More sharing options...
.josh Posted April 1, 2011 Share Posted April 1, 2011 Okay so you posted an example text, and what it's doing, can you post what you expect it to look like? Quote Link to comment Share on other sites More sharing options...
drisate Posted April 4, 2011 Author Share Posted April 4, 2011 Well i expect it to convert the key word into a link when the key word is not already part of a link. The str_replace method does not allow the check if the key word is part of a <a href or inside the link tag. so i guess the only way would be in regex Quote Link to comment Share on other sites More sharing options...
.josh Posted April 4, 2011 Share Posted April 4, 2011 well there are a million ways to write code for "a link". Can you post the actual "before" and "after" of what you physically want it to look like... Quote Link to comment Share on other sites More sharing options...
drisate Posted April 4, 2011 Author Share Posted April 4, 2011 Yeah no problem let say the keyword is "google" This would be the before: I really love google. you can access <a href="http://google.com">google.com here</a> I need the code to convert the all the google keywords but not the one part of the href and the one inside the a tag In this case only one needs to be converted. The after would look like this: I really love <A class="link2" href="index.php?mod=rep&bcode=4&icode=5&sorter=carte&anchor=6">google</a>. you can access <a href="http://google.com">google.com here</a> My current str_replace code would convert it like this with out filtring out if the given keyword is part of a link url or tag: I really love <A class="link2" href="index.php?mod=rep&bcode=4&icode=5&sorter=carte&anchor=6">google</a>. you can access <a href="http://<A class="link2" href="index.php?mod=rep&bcode=4&icode=5&sorter=carte&anchor=6">google</a>.com"><A class="link2" href="index.php?mod=rep&bcode=4&icode=5&sorter=carte&anchor=6">google</a>.com here</a> The code is used in a lot of places so thats the reson i had it inside a function url_adder($string){ The keywords are provided by a database so i need to loop them all and add the link when a match is found that is not already part of a link. Quote Link to comment Share on other sites More sharing options...
drisate Posted April 4, 2011 Author Share Posted April 4, 2011 So in anycase, the code needs to check if the keyword is not inside the folowing href=' and ' href=" and " "> and </a> if not proceed with the replace Quote Link to comment Share on other sites More sharing options...
.josh Posted April 4, 2011 Share Posted April 4, 2011 Will need to replace the replacement w/ what you want but here is an example of the actual regex and a generic replacement example. $word = 'google'; $string = 'I really love google. you can access <a href="http://google.com">google.com here</a>'; $string = preg_replace('~('.$word.')(?!(??!</?a[^>]*>).)*</a[^>]*>)(?![^<>]*>)~','<a href="$1">$1</a>',$string); echo $string; Quote Link to comment Share on other sites More sharing options...
drisate Posted April 5, 2011 Author Share Posted April 5, 2011 works perfect! function url_adder($string){ $select = mysql_query("SELECT * FROM rep_membre") or die(mysql_error()); while ($page = mysql_fetch_array($select)) { $ba = @current(@mysql_fetch_assoc(@mysql_query("SELECT batiment FROM numc where numc='$page[numc]'"))); $string = preg_replace('~('.html_entity_decode($page['entreprise']).')(?!(??!</?a[^>]*>).)*</a[^>]*>)(?![^<>]*>)~','<A class="link2" href="index.php?mod=rep&bcode='.$page[numc].'&icode='.$page[id].'&sorter=carte&anchor='.$page[id].'">'.html_entity_decode($page[entreprise]).'</a>',$string); } return $string; } Thx bro! Your da man! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.