Is everything preg_replace except numbers, letters, periods and slashes?

I have a regex that breaks everything except letters. numbers and periods. How can I add bindings to it?

$targetFile = preg_replace('/[^A-Za-z0-9-.]/', '', $targetFileDirty); 
+6
source share
3 answers

You can avoid the afterburner by preceding it with a backslash - $targetFile = preg_replace('/[^A-Za-z0-9-.\/]/', '', $targetFileDirty);

Alternatively, and perhaps better, you can use different delimiters, for example, $targetFile = preg_replace('#[^A-Za-z0-9-./]#', '', $targetFileDirty);

+18
source

To be Unicode compatible, you can use:

 $targetFile = preg_replace('#[^\pL\pN./-]+#', '', $targetFileDirty); 
+7
source

Just add a hidden forward slash: [^A-Za-z0-9-.\\/]

+2
source

Source: https://habr.com/ru/post/901970/


All Articles