简体   繁体   中英

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);

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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM