Click here to Skip to main content
15,792,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guys,
there is this reg-ex that i can seem to solve, any help would be appreciated,
i wana replace the searched word into an HREF,but avoid the nesting of HREFs, what im doin rite now, is that i place garbage '<##' and '##>' in the begining and end of the searched word in the text that has already been changed and then replace the words not in between <## and ##>

// placing <## and ##> 
$temptext = preg_replace('/(<a([^>]+)>)(.*?)(<\/a>)/is', "$1<##$3##>$4", $blogvalue["content"]);

//then search the word not in <## and ##>

$search = "/([^<##a-zA-Z'=:\"]".mysql_escape_string($value["keyword"]).")/is";

// replace it with the HREF

$replace = ' <a href="'.$value[" url="].'">'.mysql_escape_string($value["keyword"]).'</a>';

//assign it to the new content
$newContent = preg_replace($search, $replace, $temptext);

now the problem is that this also changes the link inside the HREF,
for example, if i have some link already changed in my content like
<a href="">Brown</a> and i wana change the rest of the 'Brown' in the content, it would ignore the 'Brown' inside the anchors, but would change the 'brown' in .....

any ideas

[Modified: added pre tags, though I'm not sure what language that is...the $ made me think PHP, but not with comments starting with //]
Updated 30-Jun-10 5:44am

I am not totally sure what you are trying to do, but I can't help thinking you are going about it the wrong way.
If what you are trying to do is replace the link text that is shown to the user in
<a href="">Brown</a>
so it becomes
<a href="">Green</a>
then why not try something a bit simpler:
which breaks it into groups: those with tags, and those without.
You can then only change the "data" group items.

If that isn't what you are trying to do, then please give us better detail.
Share this answer
arifeen123 30-Jun-10 12:15pm    
thanks for the reply,
what im tryna do here is that i an developing a sevice which will change some words in a blog into Anchors, but should avoid the words already between anchor tags, means it should change "Brown" but not
<a href="someURL">Brown</a> , this is very simple,and working, but as soon as i get somehting like
<a href="Brown">Brown</a> , it changes the "Brown" inside the anchor tag, i.e and puts another anchor in place of Brown, so it becomes something like
<a href="<a href="Brown">Brown</a>">Brown</a> , i hope i made it clear, if not plz do tell me
arifeen123 1-Jul-10 5:15am    
sorry about the formatting of previous comment, the editor removed all the HTML from my comment :S , its correct now
this is by Perl (just example)

#!/usr/bin/perl -w
use strict;

my $url = '<a href="">adrabi</a>';
print "$url\n";
my $q = "<##" . ( 'adrabi' ) . "##>";
$url=~s/$q/You Win/;
print $url

output :

<<a href=""><##adrabi##></a>
<<a href="">You Win</a>

just change from
$search = "/([^<##a-zA-Z'=:\"]".mysql_escape_string($value["keyword"]).")/is";


$search = "/<##".mysql_escape_string($value["keyword"])."##>/is";

for find anything in <## and ##>
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900