亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我需要 preg_match_all() 模式在方括號標簽 PHP 中獲取字符串

我需要 preg_match_all() 模式在方括號標簽 PHP 中獲取字符串

PHP
臨摹微笑 2022-11-04 16:38:35
我想解析一個字符串以獲取方括號標簽內的值:[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 個字符的數組:非常感謝您的幫助:)
查看完整描述

1 回答

?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

如果你只想匹配句子,你可以使用第一個匹配,然后是除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

)


查看完整回答
反對 回復 2022-11-04
  • 1 回答
  • 0 關注
  • 181 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號