我需要 preg_match_all() 模式在方括号标签 PHP 中获取字符串

我想解析一个字符串以获取方括号标签内的值:

[vc_column_text][/vc_column_text]

preg_match_all()在 PHP 中使用

$string = '[vc_row][vc_column][/vc_column][/vc_row][vc_row][vc_column width="1/2"][vc_column_text css=".vc_custom_1576642149231{margin-bottom: 0px !important;}"]This is the string I want to fetch[/vc_column_text][/vc_column][/vc_row]`;

我试过这个:

preg_match_all("'[vc_column_text(.*?)](.*?)[/vc_column_text]'", $string, $matches);

但这只会返回一个 2-3 个字符的数组:

http://img3.mukewang.com/6364cf9e0001395002280410.jpg

非常感谢您的帮助:)



临摹微笑
浏览 182回答 1
1回答

明月笑刀无情

如果你只想匹配句子,你可以使用第一个匹配,然后是除or[vc_column_text之外的任何字符,然后匹配结束[]]然后匹配 0+ 次出现的空白字符并捕获 1 次或多次出现的任何字符,但组 1 中的空白除外。\[vc_column_text[^][]*\]\s*(.+?)\[/vc_column_text]解释\[vc_column_text匹配[vc_column_text[^][]*\]匹配[,然后出现 0+ 次除[or]和匹配之外的任何字符]\s*匹配 0+ 个空格字符(.+?)捕获组 1,匹配任何字符 1+ 次非贪婪\[/vc_column_text]匹配[/vc_column_text]正则表达式演示| php演示示例代码$string = '[vc_row][vc_column][/vc_column][/vc_row][vc_row][vc_column width="1/2"][vc_column_text css=".vc_custom_1576642149231{margin-bottom: 0px !important;}"]This is the string I want to fetch[/vc_column_text][/vc_column][/vc_row]';preg_match_all("~\[vc_column_text[^][]*\]\s*(.+?)\[/vc_column_text]~", $string, $matches);print_r($matches[1]);输出Array(    [0] => This is the string I want to fetch)
打开App,查看更多内容
随时随地看视频慕课网APP