Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Guys,
here is the code, lets say i need to change the word "Brown" in the $str string, but avoid changing it in in the where it is written as a color in the span tag, as well as avoid the nesting of anchors where its already used, now the problem is, this solution doesnt change where it is supposed to most importantly, i.e the normal words like in the sentence "brown browinsh charlie brown" here both "brown" in the start and end should change, but they arent :S:S ... someone did this regex for me, and i am unable to fix it :(

$keyword = "BROWN";

$str = "quick brown fox jumped over the <a href="http://brown.com">lazy</a> brownish dog <p>akar bakar</p>. brown brown <a href="http://blahblah.com">brown</a> brown browinsh charlie brown. " . 'This is \'brown\'' . "<b>Brown</b> coat";

$href_str = preg_replace("/(&lt;)(\w+)([^&gt;^&lt;a]*&gt;)(".$keyword.")/ie", "'\\1'.'\\2'.'\\3'.'<a href="\"test.php\""><b>'.'\\4'.'</b></a>'", $str);

$text_str = preg_replace("/([&lt;\/\w+&gt;|&lt;\w+&gt;])( *)(^<b>|&lt;\/b&gt;)(\b".$keyword."\b)/ie", "'\\1'.'\\2'.'<a href="\"test.php\"">'.'\\3'.'</a>'", $href_str);

$dquote_str = preg_replace("/(\") *(\b".$keyword."\b)/ie", "substr('\\0', 1, 1). '<a href="\"test.php\"">'. substr('\\0', 2, strlen('\\0') - 2).'</a>'", $text_str);

$squote_str = preg_replace("/(\') *(\b".$keyword."\b)/ie", "substr('\\0', 0, 1). '<a href="\"test.php\"">'. substr('\\0', 1, strlen('\\0') - 1).'</a>'", $dquote_str);


echo $squote_str ;</b>

[Modified:

@Kunal: just adding pre tags isn't always enough. You need to actually look at it before finalizing it. Because the $str variable had <, it was treating it as HTML and not displaying properly.

@arifeen: This looks like php to me. (especially with the preg_replace). However, if that's the case, then your strings are all messed up because you're not escaping the double quotes (actually, not sure about that). With the fact that you're using regular expressions, I didn't want to go through and just escape all of the double quotes, because there were times that that seemed inaccurate.
Also, when you pasted, it seems to have converted some of the < to the html form (&lt;). I don't know if that was intentional (I doubt it), but it looks wrong. There were just too many questions for me to try to adjust it.]
Posted
Updated 9-Jul-10 8:37am
v4
Comments
William Winner 9-Jul-10 14:48pm    
personally, I wouldn't even use a regular expression to do this. I'm sure someone can write a great one to do it, but I would just write a custom function to do it.

For me, it would be a lot less headache than doing a RegEx.
E.F. Nijboer 15-Jul-10 5:00am    
If you really want to do it with a regex and are stuck, try a good tool like RegexBuddy that sort of gives you the steps in english and is great for testing these kind of regex. It also works much faster because you can also test directly.
Otherwise, do as the above and write a simple function for it. Look for each keyword "BROWN" in the text and replace all except for the condition {from the position of the keyword do a reverse search for the first occurance of a "<" and take the characters until the first " "} is equal to "pre".

Good luck!

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