如何在 Wordpress 风格的短代码之间提取字符串 a?
How do I extract string a between Wordpress-style shortcodes?
这对于 Regex 专家来说可能微不足道。可悲的是,那不是我。
给出以下 string:text:
这是一段话,[callout]这是系统的测试[/callout]。
我想使用 preg_match() and/or preg_replace 创建以下内容
<div class="callout">this is a test</div>
这就是我现在所在的位置...
$content = 'Here is a paragraph and [callout]this is a test[/callout] of the system.';
$pattern = "/[callout](.*?)[\/callout]/s";
$matches = array();
preg_match($pattern, $content, $matches);
var_dump($match[0]);
...我被卡住的地方是上面的模式似乎包含标签,即。
[callout]this is a test[/test]
...我错过了什么?
TIA,
尝试使用这个正则表达式,
\[callout\](.*?)\[\/callout\]
将从 Here is a paragraph and [callout]this is a test[/callout] of the system
capture the group this is a test
你应该转义方括号:
$pattern = "~\[callout\](.*?)\[/callout\]~s";
正如 preg_match 参考资料所说:
If matches is provided, then it is filled with the results of search. $matches[0]
will contain the text that matched the full pattern, $matches[1]
will have the text that matched the first captured parenthesized subpattern, and so on.
您可以在 $matches[1]
中找到所需的文本。
这对于 Regex 专家来说可能微不足道。可悲的是,那不是我。
给出以下 string:text:
这是一段话,[callout]这是系统的测试[/callout]。
我想使用 preg_match() and/or preg_replace 创建以下内容
<div class="callout">this is a test</div>
这就是我现在所在的位置...
$content = 'Here is a paragraph and [callout]this is a test[/callout] of the system.';
$pattern = "/[callout](.*?)[\/callout]/s";
$matches = array();
preg_match($pattern, $content, $matches);
var_dump($match[0]);
...我被卡住的地方是上面的模式似乎包含标签,即。
[callout]this is a test[/test]
...我错过了什么?
TIA,
尝试使用这个正则表达式,
\[callout\](.*?)\[\/callout\]
将从 Here is a paragraph and [callout]this is a test[/callout] of the system
this is a test
你应该转义方括号:
$pattern = "~\[callout\](.*?)\[/callout\]~s";
正如 preg_match 参考资料所说:
If matches is provided, then it is filled with the results of search.
$matches[0]
will contain the text that matched the full pattern,$matches[1]
will have the text that matched the first captured parenthesized subpattern, and so on.
您可以在 $matches[1]
中找到所需的文本。