如何使用正则表达式提取包含在 CDATA 之间的字符串部分



如何使用 php 提取包含在 CDATA 标签中的字符串?例如,我有

$str = "<![CDATA[this is my text]]>";

使用正则表达式如何提取字符串"这是我的文本"?

您可以使用此正则表达式: /<![CDATA[(.*?)]]>/

$str = "<![CDATA[this is my text]]>";
$matches = array();
preg_match('/<![CDATA[(.*?)]]>/', $str, $matches);
echo $matches[1]; // this is my text

正则表达式查找<![CDATA[后跟任何字符,直到遇到第一个]]>

如果你的字符串总是以<![CDATA[开头,以]]>结尾,你可以使用substr()

$str = "<![CDATA[this is my text]]>";
$output = substr($str,9,strlen($str)-12);
echo $output;

最新更新