Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pettturu's avatar

preg_replace issue

Hi, a small issue with regular expressions:

My result should contain only unicode alphabets and ()[]{}+-_,:&!?@#$£=;~/"'.

Here is my trial

$result = preg_replace( '/^[^\pL-0-9- "\',()\[\]{}*\/+\-_,.:&!?@#$£=*;~]*$/', ' ', $string)

Seems that < and > are not removed from $string... what am I missing? Thanks already!

0 likes
3 replies
Tray2's avatar

The dot is not escaped so it equals any character including <and >

$result = preg_replace( '/^[^\pL-0-9- "\',()\[\]{}*\/+\-_,\.:&!?@#$£=*;~]*$/', ' ', $string);

Should work better.

You can always check your regexp on https://regexr.com/

pettturu's avatar

@TRAY2 - Can't get it to work with this either...

using >string< as $string returns >string<

Also, regexer doesn't understand \pL. Any ideas for that?

pettturu's avatar
pettturu
OP
Best Answer
Level 41

This seems to do the trick

preg_replace('/[^\pL-0-9- "\',()\[\]{}*\/+,.:&!?@&#$£=*;~]/u', ' ', $string);

Prove me wrong =)

Please or to participate in this conversation.