regex help with getting tag content in PHP

后端 未结 4 1311
有刺的猬
有刺的猬 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:06

    try DOM

    $url  = "http://www.freakonomics.com/2008/09/24/wall-street-jokes-please/";
    $doc  = new DOMDocument();
    $dom  = $doc->loadHTMLFile($url);
    $items = $doc->getElementsByTagName('title');
    for ($i = 0; $i < $items->length; $i++)
    {
      echo $items->item($i)->nodeValue . "\n";
    }
    

提交回复
热议问题