使用 preg_replace 替换 php 中出现的所有内容

发布于 2024-10-11 01:26:25 字数 320 浏览 5 评论 0原文

正则表达式绝对是我的弱点,这个让我完全难住了。我正在构建一个相当基本的搜索功能,我需要能够根据以下模式更改我的用户输入:

主题:

%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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぃ弥猫深巷。 2024-10-18 01:26:25

看来您的数据是 URL 编码的。如果你应用urldecode,你会得到

"first set" "second set" -drupal -wordpress

(我假设您在 -drupal 之前有一个空格)。

现在您必须添加+。再次,我假设您必须在所有没有 - 且不在引号内的单词之前添加这些内容:

$str = '"first set" "second set" -drupal -wordpress foo';
echo preg_replace('#( |^)(?!(?:\w+"|-| ))#','\1+', $str));
// prints +"first set" +"second set" -drupal -wordpress +foo

更新: 如果您无法使用 urldecode,您可以使用 str_replace%22 替换为 "

Seems your data is URL encoded. If you apply urldecode, you will get

"first set" "second set" -drupal -wordpress

(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:

$str = '"first set" "second set" -drupal -wordpress foo';
echo preg_replace('#( |^)(?!(?:\w+"|-| ))#','\1+', $str));
// prints +"first set" +"second set" -drupal -wordpress +foo

Update: If you cannot use urldecode, you could just use str_replace to replace %22 with ".

送舟行 2024-10-18 01:26:25
preg_replace('/%22((?:[^%]|%[^2]|%2[^2])*)%22/', '+"$1"', $str);

说明: $1 是反向引用,它引用正则表达式中的第一个 () 部分,在本例中为 ((?:[^%] |%[^2]|%2[^2])*)。以及 [^%] 和其后的交替 (...|...|...) 防止其间的 %22由于贪婪而被匹配。请参阅http://en.wikipedia.org/wiki/Regular_expression#Lazy_quantification

我在匹配块注释 (/* */) 的 JavaCC 示例中发现了该技术,并且我找不到任何其他网页对其进行解释,因此这里有一个更清晰的示例: 12345 12345........12345 之间的文本,中间没有 12345:/12345([^1]|1[^2]|12[^3]|123 [^4]|1234[^5])*12345/

preg_replace('/%22((?:[^%]|%[^2]|%2[^2])*)%22/', '+"$1"', $str);

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 12345 12345........12345 with no 12345 in between: /12345([^1]|1[^2]|12[^3]|123[^4]|1234[^5])*12345/

影子的影子 2024-10-18 01:26:25

这是您要找的吗?

<?php
  $input = "%22first set%22 %22second set%22-drupal -wordpress";
  $res = preg_replace( "/\%22(.+?)\%22/","+\"(\\1)\" ", $input);
  print $res;
?>

Is this what you're looking for?

<?php
  $input = "%22first set%22 %22second set%22-drupal -wordpress";
  $res = preg_replace( "/\%22(.+?)\%22/","+\"(\\1)\" ", $input);
  print $res;
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文