djjmz
djjmz

Reputation: 9

How to use preg_replace with special symbols

I have problem with preg_replace. How to leave these symbols: . , ? ! ' " : ; and remove others? This function used with Lithuanian letters and numbers. I have tried this code:

preg_replace('/[^\p{L}\p{N}\s !?,;:.-]/u', '', $value);

Upvotes: 0

Views: 79

Answers (1)

Jinksy
Jinksy

Reputation: 451

You have to escape those characters that have a special meaning in regular expression in that case.

preg_replace ('/[^\.,?!\'":;\-]/', '' ,$value);

preg_quote can also be used:

$toKeep = preg_quote ('.,?!\'":;', '/');
preg_replace ('/[^' . $toKeep . ']/', '', $value); 

Upvotes: 2

Related Questions