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;