regex help with getting tag content in PHP

后端 未结 4 1351
有刺的猬
有刺的猬 2021-01-28 03:47

so I have the code

function getTagContent($string, $tagname) {

    $pattern = \"/<$tagname.*?>(.*)<\\/$tagname>/\";
    preg_match($pattern, $string         


        
4条回答
  •  难免孤独
    2021-01-28 04:13

    The 'title' tag is not on the same line as its closing tag, so your preg_match doesn't find it.

    In Perl, you can add a /s switch to make it slurp the whole input as though on one line: I forget whether preg_match will let you do so or not.

    But this is just one of the reasons why parsing XML and variants with regexp is a bad idea.

提交回复
热议问题