正则表达式匹配包含文本双引号和方括号的字符串

时间:2022-09-13 07:51:59

I am trying to extract the text between two strings with double quotes and square brackets. [gallery ids=" and "]

我试图用双引号和方括号在两个字符串之间提取文本。 [gallery ids =“and”]

The format I have is:

我的格式是:

[gallery ids="55,57,56,58,59"]

Which I would like to turn in to

我想转向

55,57,56,58,59

I have tried every variety of pattern I've come across but have had no luck. Can anyone tell me what pattern would achieve this using PHP's regex functions?

我尝试了各种各样的模式,但我没有运气。任何人都可以告诉我使用PHP的正则表达式函数可以实现什么模式?

1 个解决方案

#1


3  

That should work for you:

这应该适合你:

$string = '[gallery ids="55,57,56,58,59"]';
if (preg_match('/\[gallery\sids="([^"]+)"\]/', $string, $m)) {
    echo $m[1];
}

or if you want to match more than one string like that in the text, then just use preg_match_all:

或者如果你想匹配文本中的多个字符串,那么只需使用preg_match_all:

if (preg_match_all('/\[gallery\sids="([^"]+)"\]/', $string, $m)) {
    print_r($m[1]);
}

#1


3  

That should work for you:

这应该适合你:

$string = '[gallery ids="55,57,56,58,59"]';
if (preg_match('/\[gallery\sids="([^"]+)"\]/', $string, $m)) {
    echo $m[1];
}

or if you want to match more than one string like that in the text, then just use preg_match_all:

或者如果你想匹配文本中的多个字符串,那么只需使用preg_match_all:

if (preg_match_all('/\[gallery\sids="([^"]+)"\]/', $string, $m)) {
    print_r($m[1]);
}