jartaud
jartaud

Reputation: 385

Help with preg_replace and special chars

I'm using the PDO class but I'm triying to remove all chars except...:

function cleaner($str){
    return preg_replace('/[^a-zA-Z0-9éàêïòé\,\.\']/',' ',trim($str));
}

As you can see, it's a simple function, but it removes all chars éàêïòé

example: cleaner('$#$<<>-//La souris a été mangée par le chat ') //returns

La souris a t mang e par le chat (The mouse has been eaten by the cat :) )

Any help will be appreciate

Upvotes: 2

Views: 1246

Answers (2)

avpaderno
avpaderno

Reputation: 29679

$str = '$#$<<>-//La souris a été mangée par le chat ';
$str = preg_replace('/[^a-zA-Z0-9éàêïòé\,\.\']/u',' ',trim($str));

$str = '$#$<<>-//La souris a été mangée par le chat ';
$str = preg_replace('/[^\p{L}\,\.\']/u',' ',trim($str));

Both the snippets worked for me, on PHP 5.3. The second regular expression is less restricted, and accepts all Unicode letters.

Upvotes: 1

m1tk4
m1tk4

Reputation: 3467

You need to add /u pattern modifier to your pattern to turn on UTF-8 support in PCRE. This is assuming everything is in UTF-8 already.

http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php

Upvotes: 2

Related Questions