从PHP中的短代码获取内容

时间:2018-06-04 13:07:05

标签: php regex

如何从PHP中的短代码获取内容MY_TEXT?我有很多短代码,所以我想删除所有,并获得干净的文本。内容来自Wordpress,但我构建自己的外部脚本,所以我没有WP功能。

这是我的例子:

[et_pb_text admin_label="text" _builder_version="3.0.106" custom_margin="|||" custom_padding="||0px|"]
MY_TEXT
[/et_pb_text]

1 个答案:

答案 0 :(得分:1)

您可以使用正则表达式来匹配标记中的内容: @\[([^ /]+).+\](.+)\[/\1\]@gms

$re = '@\[([^ /]+).+\](.+)\[/\1\]@ms';
$str = '[et_pb_text admin_label="text" _builder_version="3.0.106" custom_margin="|||" custom_padding="||0px|"]
MY_TEXT
[/et_pb_text]';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);

https://regex101.com/r/FIzTuu/1

相关问题