11 Vote

PHP: Case Insensitive String Replacements

Question by Guest | 2014-06-27 at 13:03

For searching and replacing strings, up to now I have always used the function str_replace in PHP.

Unfortunately, now I am coming to the limits of this function, because I would like to do some replacements independent from uppercase and lowercase writing of the letters and characters.

Of course, I also know about regular expressions and that you can use them in this case, but I am a PHP beginner so that this is a bit complicated for me at that time. Isn't there any simple way to do case insensitive string replacements in PHP?

ReplyPositiveNegative
1Best Answer1 Vote

Instead of working with complicated regular expressions, you can also use the function str_ireplace instead of str_replace. This is a function working exactly like str_replace with the only difference is that it is replacing without caring about upper and lower case of writing.

$s = 'Hallo';
$s = str_ireplace('hallo', 'test', $s);
echo $s;  // test

It is even better for the performance and it is recommended to use this function instead of regular expressions (preg_replace() with the i-modifier).
2014-06-29 at 15:09

ReplyPositive Negative
Reply

Related Topics

HTACCESS: Simplify URL

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.