neptune
neptune

Reputation: 1261

Regex issue with mb_ereg_replace

I have this code which should remove non letter characters from a string:

<?php
$text = 'Random -text! and a word with many ppppps';
$text = mb_ereg_replace('[^\p{L} ]', ' ', $text);
echo $text;
?>

When I run it on localhost (php 5.6) returns what I expect:

Random text and a word with many ppppps

But on godaddy (php 5.3) it only returns:

ppppp 

Seems to be something with the regexp '[^\p{L} ]' in mb_ereg_replace but I can't figure out what.

Upvotes: 1

Views: 298

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626870

As per you comment, it is clear that the godaddy server was not set to work with UTF8 encoding. So, after adding

ini_set('default_charset', 'UTF-8');

you may use

$text = preg_replace('/[^\p{L}\p{M} ]/u', ' ', $text);

See IDEONE demo proving it works with UTF8.

Upvotes: 1

Related Questions