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
Share on other sites

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

Link to comment
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.