2

I've got the following code which works fine on my offline test version but it fails on the online server.

$names = "dimitris giannIs micHalis";
echo preg_replace("/s\b/", "w", mb_convert_case($names, MB_CASE_TITLE, "UTF-8"));

The result I get is Dimitriw Gianniw Michaliw.

But instead of English chars/words I've got UTF-8 ones. If I place the above example as it is (in English) it works fine so I'm guessing I'm doing something wrong here with UTF-8

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
George M.
  • 63
  • 6
  • run `phpinfo()` and check for the mbstring extension –  Aug 23 '18 at 22:21
  • @IdontDownVote It looks similar with the offline version. Should I show you something specific? – George M. Aug 23 '18 at 22:25
  • i dont do a lot of multi byte sting manipulation i just know the extensions may not always be installed. as it works on one set-up for you but not another its a good bet that its an issue with the php installation itself. Hopefully you have already been through theses links: http://php.net/manual/en/mbstring.setup.php –  Aug 23 '18 at 22:30

1 Answers1

4

Typically (but see the note below the Edit), you need to use the u modifier on your regex to make it work with UTF-8 characters. e.g.

$words = "qθαεqθε γραεcισ cονσεcτε";
echo preg_replace("/ε\b/u", "α", mb_convert_case($words, MB_CASE_TITLE, "UTF-8"));

Output:

Qθαεqθα Γραεcισ Cονσεcτα

This example on rextester demonstrates the use of the u modifier (note that rextester doesn't support mb_convert_case but that doesn't really affect the result).

Edit

As was pointed out by @CasimiretHippolyte, it is possible to compile the PCRE extension (used by PHP for regex) to handle unicode characters by default with the --enable-unicode-properties option. This may explain the difference between the results on the offline test version and online server.

Nick
  • 138,499
  • 22
  • 57
  • 95
  • not saying you are wrong, but OP said it worked on his off-line version. –  Aug 23 '18 at 23:47
  • 1
    @Nick: Even if your answer can be useful and solve the problem, your comment is wrong since the pcre extension (used by php for regex) can be compiled to handle unicode characters **by default** (with `--enable-unicode-properties` option) and extends `\b` to unicode characters without to add the u modifier or the `(*UCP)` at the start of the pattern. That is probably why it works with the online test and not with the server. – Casimir et Hippolyte Aug 24 '18 at 02:33
  • @CasimiretHippolyte I did not know that. Thank you for enlightening me. I will remove the comment and add an edit to the post. – Nick Aug 24 '18 at 02:50