1
我有以下的正則表達式:在URL淨化的正則表達式中包含反斜槓?
$url = "http://example.com?param=test1\test2\test3\test4";
$cleanUrl = preg_replace('|[^a-z0-9-~+_.?\[\]\^#=!&;,/:%@$\|*`\'<>"()\\x80-\\xff\{\}]|i', '', $url);
我得到以下輸出:
但是, 我期待下面的輸出:
http://example.com?param=test1 \ TEST2 \ TEST3 \ TEST4
我想這正則表達式,但它不工作:
$cleanUrl = preg_replace('|[^a-z0-9-~+_.?\[\]\^\\#=!&;,/:%@$\|*`\'<>"()\\x80-\\xff\{\}]|i', '', $url);
^escaped single quote
你確定輸入字符串?請參閱https://ideone.com/OnepGA。我認爲它應該是'$ url =「http://example.com?param=test1\\test2\\test3\\test4」;'。然後使用'$ cleanUrl = preg_replace('| [^ - \\\\ a-z0-9〜+ _。?\ [\] \ ^#=!&;,/:%@ $ \ | * \' '<>「()\ x80- \ xff \ {\}] | i','',$ url);'。請參閱[此演示](https://ideone.com/tUHtU3)。 –
您必須使用\\\\來轉義反斜線。閱讀此[SO答案](http://stackoverflow.com/a/4025505/5447994) – Thamilan