如何仅转义某些字符 [英] How to escape only certain characters
问题描述
php函数 preg_quote
会转义这些字符。 \ + *? [^] $(){} =! < > | :--
php function preg_quote
escapes these characters . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -
能否请您提出我可以用来转义这些字符的方法: +-&& || ! (){} [] ^〜*?:/
Could you please suggest what I could use to escape these characters: + - && || ! ( ) { } [ ] ^ " ~ * ? : /
已编辑(按保留的要求):
我正在执行 Solr
搜索并它说:
I'm implementing a Solr
search and it says:
Solr在查询中出现
时赋予以下字符特殊含义:
+-&& ||!(){} [] ^〜*吗? :/
Solr gives the following characters special meaning when they appear in a query:
+ - && || ! ( ) { } [ ] ^ " ~ * ? : /
要使Solr从字面上解释任何这些字符,而不是将其作为特殊字符,请在
字符前加反斜杠字符。
To make Solr interpret any of these characters literally, rather as a special character, precede the character with a backslash character .
推荐答案
这可能会帮助您:
<?php
$subject = "+ - && || ! ( ) { } [ ] ^ \" ~ * ? : /";
$result = preg_replace('%([+\-&|!(){}[\]^"~*?:/]+)%', '\\\\$1', $subject);
echo $result;
?>
这篇关于如何仅转义某些字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!