-1

I'm having problems with mb_strtoupper. I need it to capitalize a text that is in a newsletter generated in PHP (so I cant just capitalize it with css, and is stuck using a PHP function that will capitalize the text)

Thing is, mb_strtoupperworks fine, but has a problem with the quote ' character. A text like Bob's Burgers is being converted to BOB’S BURGERS

Is there a way to make mb_strtoupper leave the quote alone?

Fredy31
  • 2,660
  • 6
  • 29
  • 54

2 Answers2

1

That's because your text is not Bob's Burgers but Bob’s Burgers. In other words, you don't have plain text but HTML and mb_… functions do not have builtin HTML parsers.

It can be really tricky because HTML is a full-fledged language. If you do not expect HTML tags (e.g. Click <a href="//example.com">here</a>) you can try something like this:

$data = 'Bob&rsquo;s Burgers';
// 1. Convert to plain text
$data = mb_convert_encoding($data, 'UTF-8', 'HTML-ENTITIES');
// 2. Upper case
$data = mb_strtoupper($data, 'UTF-8');
// 3. Encode back to 7-bit ASCII
$data = mb_convert_encoding($data, 'HTML-ENTITIES', 'UTF-8');
var_dump($data);
string(19) "BOB&rsquo;S BURGERS"

(Online demo)

Step #3 should not really be necessary but I guess you have HTML entities for some reason :-?

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • I'd wager that the entities are there because OP's app "has trouble with special characters being mangled" due to encoding mismatches. https://stackoverflow.com/questions/279170/utf-8-all-the-way-through – Sammitch Jul 11 '18 at 17:48
  • @Sammitch Certainly, HTML entities must be already there, there's no way to produce them out of the blue with `mb_strtoupper()`. People don't always make a clear distinction between HTML source code and the rendered view. The answer by WebCode.ie is wrong and even the OP acknowledged so—right after accepting it... – Álvaro González Jul 11 '18 at 17:52
  • The Green Checkmark of Ultimate Truth applied to an incorrect answer?! Hold the line while I light the beacons and call for aid! – Sammitch Jul 11 '18 at 18:25
0

set the encoding as the second parameter in mb_strtoupper like so...

mb_strtoupper("Bob's Burgers", 'UTF-8');

More info here: http://php.net/manual/en/function.mb-strtoupper.php

Simon K
  • 1,503
  • 1
  • 8
  • 10
  • Copy pasting the Bob's Burgers from this answer works, but not when I'm doing shift+, myself. Maybe my french canadian keyboard is dumb and is not giving me the same exact character. I fixed it by a ducktape way, doing a str_replace. $titleCap = str_replace('&RSQUO;',"'",$titleCap); – Fredy31 Jul 11 '18 at 17:38