在PHP中反转preg_quote

ibr*_*him 3 php regex inverse

我需要编写一个与preg_quote函数完全相反的函数.简单地删除所有'\'都不起作用,因为字符串中可能有'\'.

例;

inverse_preg_quote('an\\y s\.tri\*ng') //this should return "an\y s.tri*ng" 
Run Code Online (Sandbox Code Playgroud)

或者你可以测试为

inverse_preg_quote(preg_quote($string)) //$string shouldn't change
Run Code Online (Sandbox Code Playgroud)

Wil*_*der 5

你正在寻找striplashes

<?php
    $str = "Is your name O\'reilly?";

    // Outputs: Is your name O'reilly?
    echo stripslashes($str);
?>
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅http://php.net/manual/en/function.stripslashes.php.(还有更多功能的http://www.php.net/manual/en/function.addcslashes.phphttp://www.php.net/manual/en/function.stripcslashes.php你可能想要调查 )

编辑:否则,您可以执行三次str_replace调用.第一个用$ DOUBLESLASH替换\\,然后将\替换为""(空字符串),然后将$ DOUBLESLASH设置回\.

$str = str_replace("\\", "$DOUBLESLASH", $str);
$str = str_replace("\", "", $str);
$str = str_replace("$DOUBLESLASH", "\", $str);
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅http://php.net/manual/en/function.str-replace.php.