PHP: Make a string upper case but not the html entities in it?

I cannot use the CSS variant by kapa, 'cause I need this for the title tag. The solution provided by ircmaxell might be right, but since some servers don't have mbstring extension, this solution might be better:

echo htmlentities(strtoupper(html_entity_decode($str)));

I know you haven't listed CSS in your tags, but most of the time it is easier to leave this to the client side (if you only intended this string for browser display).

Applying CSS text-transform: uppercase; will do this for you.


It is better to convert string to uppercase first than decode than you will get desired result

$var=<i>This</i><u>is</u><b>test</b><br>New line

function uppercase strtoupper($var); Output will be

<I>THIS</I><U>IS</U><B>TEST</B><BR>NEW LINE

function htmlEntities() $var=htmlEntities($var); OUTPUT:

&lt;I&gt;THIS&lt;/I&gt;&lt;U&gt;IS&lt;/U&gt;&lt;B&gt;TEST&lt;/B&gt;&lt;BR&gt;NEW LINE

FINAL Encoding $var=html_entity_decode($var); output:

THISISTEST
NEW LINE

If first htmlentities convert than upper case; decode will fail as encoded test changed to uppercase and function failed;


Well, remove the entities and use a multi-byte character set!

$string = html_entity_decode($string, ENT_COMPAT, 'UTF-8');
$string = mb_convert_case($string, MB_CASE_UPPER, 'UTF-8');

Then output the string. There's no need for most html entities, just use the native characters and set the output of the document properly.

If you really must use the entities, a regex is in order:

$callback = function($match) {
    return strtoupper($match[1]);
}
$regex = '/(\w+(?=&)|(?<=;)\w+)/';
$string = preg_replace_callback($regex, $callback, $string);

Note that I haven't tested that regex, but it should work since it's looking for letters that are not immediately followed by a ; character...