duanhegn231318 2012-06-13 05:42
浏览 511
已采纳

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

How can I extract a string enclosed in CDATA tag using php? For instance I have

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

Using regex how can I extract the string "this is my text"?

  • 写回答

2条回答 默认 最新

  • dowm41315 2012-06-13 05:47
    关注

    You can use this regex: /<!\[CDATA\[(.*?)\]\]>/:

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

    The regex looks for <![CDATA[ followed by any characters until the first ]]> is encountered.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?