Matching SRC attribute of IMG tag using preg_match

前端 未结 6 1663
走了就别回头了
走了就别回头了 2020-12-03 08:06

I\'m attempting to run preg_match to extract the SRC attribute from the first IMG tag in an article (in this case, stored in $row->introtext).

preg_match(\'/         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-03 08:41

    Try:

    include ("htmlparser.inc"); // from: http://php-html.sourceforge.net/
    
    $html = 'bla Inside Otakuzoku\'s store noise  foo';
    
    $parser = new HtmlParser($html);
    
    while($parser->parse()) {
        if($parser->iNodeName == 'img') {
            echo $parser->iNodeAttributes['src'];
            break;
        }
    }
    

    which will produce:

    images/stories/otakuzoku1.jpg
    

    It should work with PHP 4.x.

提交回复
热议问题