使用 preg_replace 替换 php 中出现的所有内容
正则表达式绝对是我的弱点,这个让我完全难住了。我正在构建一个相当基本的搜索功能,我需要能够根据以下模式更改我的用户输入:
主题:
%22first set%22 %22second set%22-drupal -wordpress
所需的输出:
+"first set" +"second set" -drupal -wordpress
我希望我可以提供更多帮助,因为我通常喜欢至少发布我拥有的解决方案很远,但在这一点上我不知所措。
任何帮助表示赞赏。谢谢。
Regex is absolutely my weak point and this one has me completely stumped. I am building a fairly basic search functionality and I need to be able to alter my user input based on the following pattern:
Subject:
%22first set%22 %22second set%22-drupal -wordpress
Desired output:
+"first set" +"second set" -drupal -wordpress
I wish I could be more help as I normally like to at least post the solution I have so far, but on this one I'm at a loss.
Any help is appreciated. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来您的数据是 URL 编码的。如果你应用
urldecode
,你会得到(我假设您在
-drupal
之前有一个空格)。现在您必须添加
+
。再次,我假设您必须在所有没有-
且不在引号内的单词之前添加这些内容:更新: 如果您无法使用
urldecode
,您可以使用str_replace
将%22
替换为"
。Seems your data is URL encoded. If you apply
urldecode
, you will get(I assume you have a space before
-drupal
).Now you have to add
+
. Again, I assume you have to add those before all words that don't have a-
and that are not inside quotes:Update: If you cannot use
urldecode
, you could just usestr_replace
to replace%22
with"
.说明:
$1
是反向引用,它引用正则表达式中的第一个()
部分,在本例中为((?:[^%] |%[^2]|%2[^2])*)
。以及[^%]
和其后的交替(...|...|...)
防止其间的%22
由于贪婪而被匹配。请参阅http://en.wikipedia.org/wiki/Regular_expression#Lazy_quantification。我在匹配块注释 (
/* */
) 的 JavaCC 示例中发现了该技术,并且我找不到任何其他网页对其进行解释,因此这里有一个更清晰的示例: 1234512345........12345
之间的文本,中间没有 12345:/12345([^1]|1[^2]|12[^3]|123 [^4]|1234[^5])*12345/
Explanation: The
$1
is a backreference, which references the first()
-section in the regular expression, in this case,((?:[^%]|%[^2]|%2[^2])*)
. And the[^%]
and the alternations(...|...|...)
after it prevents%22
in between from being matched due to greediness. See http://en.wikipedia.org/wiki/Regular_expression#Lazy_quantification.I found that technique in a JavaCC example of matching block comments (
/* */
), and I can't find any other webpages explaining it, so here is a cleaner example: To match a block of text between 1234512345........12345
with no 12345 in between:/12345([^1]|1[^2]|12[^3]|123[^4]|1234[^5])*12345/
这是您要找的吗?
Is this what you're looking for?