Alex Rivera | Logout

str_ireplace() with keeping of case

Asked 2010-08-15T23:50:17.247
39

How can I use str_ireplace (or something similar) to replace some text for formatting and then return it with the same caps?

Example:

$original="The quick red fox jumps over the lazy brown dog.";
$find="thE";

print str_ireplace($find,'<b>'.$find.'</b>',$original);

That will output: thE quick red fox jumps over the lazy brown dog.

I want it to keep the original case and only apply the formatting, in this example, bold text.

Thank you.

Edit
Report

1 Answer

4

Either replace with the exact word, or use preg_replace:

preg_replace('/(The)/i', "<strong>$1</strong>", $original);
answered 2010-08-15T23:58:12.207

Your Answer