Hello, I'm looking for PHP function that do matching between specific word ($search_word) and all the words in my html file ($content = strtolower(file_get_contents($folder.$file)) if the function find matching it replace the word in the html text with another word. The problem is with capital letters - there won't be any matching if the words in "$content" will begin with capital letters (or will be all with big letters). Is there any function that do matching while it ignore capital letters?
You can use preg_replace with the 'i' modifier. <?php $content = "Hello, im testing with caps; Hello!"; $search_word = "hello"; $another_word = "bye"; echo preg_replace("/\b".$search_word."\b/i", $another_word, $content); ?> PHP:
Thanks danx10! One more thing - I have function that run over string that includes HTML tags and replace the word "php" (as it is) with "asp". if the text "myphp" will be found, the script won't replace it to myphp. The problem is with the HTML tags - I need to replace only the texts between the HTML tags. for example - if the string is: <a href='http://www.php.com'>myphp</a> best <b>php</b> website <h1>PhP!!!</h1> php and myphp or phpme - <u>php!</u>! PHP: The result will be: <a href='http://www.php.com'>myphp</a> best <b>asp</b> website <h1>asp!!!</h1> aspand myphp or phpme - <u>asp!</u>! PHP: The script that I already have is: 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; PHP: What should I change if I want the text between the HTML tags to be replaced?
Hi,try this code: echo preg_replace('#(\>.*?)\b(php)\b(.*?\<)#misux','$1 asp $3',$string)."\n"; PHP: Regards
Hi koko5, Thanks for your help! The input was: <a href='http://www.php.com'>myphp</a> best <b>php</b> website <h1>PhP!!!</h1> php and myphp or phpme - <u>php!</u>! <img src="" alt="great Php" /> PHP: wnd the output is: <a href='http://www. asp .com'>myphp</a> best <b> asp </b> website <h1> asp !!!</h1> asp and myphp or phpme - <u> asp !</u>! <img src="" alt="great asp " /> PHP: so it didn't work... This is the my script: 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; PHP: I changed the line before the last one into the code you gave me, without any success...:/
i have same problem, it's about regular expression..., does anybody have a referencies to learn deeper about it??
Why don't you simply do this? $output=str_ireplace(" $search_word ", $another_word, $output); PHP: It'd be much cleaner and efficient to use ''.$search_word.'' instead of " $search_word ", but this way you can clearly see what I meant. Hope it helps!
Hi HostingProvider, It won't work - a. I want to replace the exact work -"php" (no matter if there are capital letters) with the word "asp", so the word "myphp", for example, won't be replace. only clear "php". b. I don't want that text inside the HTML tags will be change. only the text between the HTML tags. for example - <a href='http://www.php.com'>php</a> - only the "php" between the <a> and </a> need to be change. your function alone can't do that...:/