Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How can I make the content in a string to upper case but not the html entities in it? Is it possible?

$str = 'FUNDA MENTALISM';
echo strtoupper($str);

I want to produce this,

'FUNDA MENTALISM'

but I get this with strtoupper()

'FUNDA MENTALISM'
like image 823
Run Avatar asked Jan 21 '11 12:01

Run


Video Answer


2 Answers

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.

like image 144
kapa Avatar answered Oct 06 '22 01:10

kapa


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...

like image 36
ircmaxell Avatar answered Oct 06 '22 01:10

ircmaxell