Web Hosting Talk







View Full Version : Replace One Thing With Another? (PHP)


P-nut
07-26-2005, 11:27 AM
I want to put something in a script I'm working on that looks for all instances of img/ and replaces it with noimg/

What would be the easiest way to accomplish this?

Thanks!

maxymizer
07-26-2005, 11:31 AM
$yourstring = str_replace('img/', 'noimg/', $yourstring);

http://www.php.net/str_replace for more info.

a2hosting
07-26-2005, 11:14 PM
I'd recommend 'str_ireplace' which has the same syntax as str_replace, but is case-insensitive. Since many people mix case in HTML, this could prove helpful.

Koobi
07-27-2005, 12:46 PM
but if you don't use PHP5, you can't use str_ireplace() but here's a fix for that:


<?php

$string = 'Hello World!';
echo myStr_ireplace('hello', 'Goodbye', $string);

function myStr_ireplace($search, $replace, $subject)
{
return str_replace(strtolower($search), $replace, $subject);
}
?>

ccole
07-28-2005, 04:11 AM
If you need it to be more complicated, then you can do regular expressions with preg_replace()