简体   繁体   English

如何组合两个(或多个)preg_replace's - PHP

[英]How can I combine two (or more) preg_replace's - PHP

< EDIT > < 编辑 >

I Wanted to give a big thank you to all of you who have provided an answer, You have all given me great options.我想非常感谢所有提供答案的人,你们都给了我很好的选择。 I will play with each and see which one suits my exact needs prior to selecting an answer.在选择答案之前,我将与每个人一起玩,看看哪一个适合我的确切需求。 I do appreciate all of your responses though, everyone has a different approach to things.虽然我很欣赏你的所有回应,但每个人对事情的态度都不一样。 =) =)

bbcodes can (but not always do) include options, an example would be: bbcodes 可以(但不总是)包括选项,一个例子是:

[URL="http://google.com"]GOOGLE[/URL]

</ EDIT > </ 编辑 >

I am currently hiding multiple variables from my string using the following method:我目前使用以下方法从我的字符串中隐藏多个变量:

$result = preg_replace('/\[img\](.*)\[\/img\]/im', 'REPLACED', $qry);
$result = preg_replace('/\[url.*\](.*)\[\/url\]/im', 'REPLACED', $qry);

This works, but I would like to be able to do this with just one preg replace, and easily be able to add to it in the future if needed.这是有效的,但我希望能够只用一个 preg 替换来做到这一点,并且如果需要,将来可以轻松地添加到它。

I have attempted to use:我曾尝试使用:

$result = preg_replace("#\[/?(img|url)(=\s*\"?.*?\"?\s*)?]#im", 'REPLACED', $qry);

Which is only hiding the tags themselves.这只是隐藏标签本身。

How can I combine these replaces?我怎样才能组合这些替换?

preg_replace function can take array of arguments. preg_replace 函数可以接受参数数组。 As an option, you could try with:作为一种选择,您可以尝试:

$find = ['/\[img\](.*)\[\/img\]/im', '/\[url.*\](.*)\[\/url\]/im'];
$replace = ['REPLACED', 'REPLACED'];

$result = preg_replace($find, $replace, $qry);

I think this way you will have some flexibility to have different replaced values我认为这样你会有一些灵活性来拥有不同的replaced

Your attempt is very close but you need to use a back reference to first capture group for closing match.您的尝试非常接近,但您需要使用对第一个捕获组的反向引用来结束匹配。

$result = preg_replace('~\[(img|url)\](.*)\[/\1\]~i', 'REPLACED', $qry);

Demo: https://3v4l.org/Ybvh2演示: https : //3v4l.org/Ybvh2

The m modifier also has no functionality without an ^ or $ .如果没有^$m修饰符也没有任何功能。 Perhaps you want the s modifier so the .也许您想要s修饰符,所以. extends to newlines?扩展到换行符? https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php

I also would change the * quantifier to be non-greedy:我也会将*量词更改为非贪婪:

$result = preg_replace('~\[(img|url)\](.*?)\[/\1\]~is', 'REPLACED', $qry);

consider https://3v4l.org/8SdNd vs. https://3v4l.org/MC0pf (non greedied).考虑https://3v4l.org/8SdNdhttps://3v4l.org/MC0pf (非贪婪)。

If you need to distinguish between images and URLs it is probably easiest to have 2 regexs.如果您需要区分图像和 URL,使用 2 个正则表达式可能最容易。 Functionality with 1 regex though can be achieved with preg_replace_callback :尽管可以使用preg_replace_callback实现具有 1 个正则表达式的功能:

$result = preg_replace_callback('~\[(img|url)\](.*?)\[/\1\]~is', function($match){
        return $match[1] =='url' ? 'REPLACED url' : 'REPLACED img';
        }, '[img]test[/img][not]img[/not][url]http][/url]');
echo $result;

You can use您可以使用

preg_replace('~\[(img|url)\b[^]]*](.*?)\[/\1]~is', 'REPLACED', $qry);

The regex matches:正则表达式匹配:

  • \\[ - a [ char \\[ - 一个[字符
  • (img|url) - Group 1: img or url (img|url) - 第 1 组: imgurl
  • \\b - a word boundary \\b - 单词边界
  • [^]]* - 0 or more chars other than ] [^]]* - 0 个或多个字符,而不是]
  • ] - a ] char ] - 一个]字符
  • (.*?) - Group 2: any zero or more chars, as few as possible (.*?) - 第 2 组:任何零个或多个字符,尽可能少
  • \\[/\\1] - [/ , the same contents as in Group 1, and a ] . \\[/\\1] - [/ ,与第 1 组中的内容相同,还有一个]

See the PHP demo :请参阅PHP 演示

$qry = '[url="google.com"]google[/url] and [img]image here[/img]';
echo preg_replace('~\[(img|url)\b[^]]*](.*?)\[/\1]~is', 'REPLACED', $qry);
// => REPLACED and REPLACED

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM