Forum Moderators: coopster

Message Too Old, No Replies

regular expressions

         

roice

2:57 pm on Mar 1, 2010 (gmt 0)

10+ Year Member


Hello everyone,
My name is Roi and I need your help.
Hope you will be able to help me-
I wrote a script that take variable that contain HTML code,. it replace the exact word "php" (no matter if it is contain capital letters) with the word "asp"
for example, if the variable contain:
[code] <a href='http://www.php.com'>myphp</a> best <b>php</b> website <h1>PhP!</h1> php and myphp or phpme - <u>php!</u>! [/code]

the result will be:
[code] <a href='http://www.asp.com'>myphp</a> best <b>asp</b> website <h1>asp!</h1> aspand myphp or phpme - <u>asp!</u>! [/code]

well, the problem is that it althouth replace the letters inside the THML tags and because of that the links changed...
Here is my code:
[code] function keepcase($word, $replace) {
$replace[0] = (ctype_upper($word[0]) ? strtoupper($replace[0]) : $replace[0]);

return $replace;
}

$text = strtolower(file_get_contents($folder.$file));
$replace = "asp";
$word = "php";
$output = preg_replace('/\b' . preg_quote($word) . '\b/ei', "keepcase('\\0', '$replace')", $text);

echo $output; [/code]

What should I change if I want the replacements to be only on the text between the HTML tags?

Thank you in advance,
Roi.

eelixduppy

9:53 pm on Mar 1, 2010 (gmt 0)



Hello and Welcome to WebmasterWorld!

This is a tough pattern to write, and odds are you probably won't be able to get it working exactly without some issue because there is always the case that the HTML is not properly formatted in the string. I'm going to mess around with this a bit and see what I can do for you.

roice

7:39 am on Mar 2, 2010 (gmt 0)

10+ Year Member



Hi eelixduppy.
Thank for the welcome and for your help!