Jump to content

Find and replace


drisate

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/232288-find-and-replace/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/232288-find-and-replace/#findComment-1196687
Share on other sites

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;

Link to comment
https://forums.phpfreaks.com/topic/232288-find-and-replace/#findComment-1196832
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/232288-find-and-replace/#findComment-1197154
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.