As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center提供指导。




7年前关闭。




我对正则表达式不熟悉,并希望对此进行解密。
return preg_replace("/[<>]/", '_', $string);

谢谢!!

最佳答案

这意味着“用下划线替换字符串<中的每个>$string,然后返回结果”。

斜杠(/)分隔正则表达式。您可以改用其他字符(如果您的正则表达式本身包含斜杠,那么preg_replace("#[<>]#", '_', $string);也可以使用,并且很有意义)。
[]括号表示字符类。它们的意思是“类中包含的一个字符”,因此[<>]的意思是“要么<要么>”。

您也可以通过以^开头字符类来否定字符类:[^<>]表示“除<>之外的任何字符。

10-07 14:01
查看更多