How can I only get the alt attribute of the a p tag in PHP?

前端 未结 1 1491
一向
一向 2021-01-19 23:31

I\'m working on a script that echo\'s only the price. If I do:

$alttag = $oNode[\'p\'];
echo $alttag;

It will echo everything in <

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-20 00:11

    So is this basically a web crawler for prices? I would suggest you look into using PHP's DOMDocument library to parse XML (Which XHTML practically is). You could then do something like:

    //create a new DOMDocument object
    $xmlDoc = new DOMDocument();  
    //load your html for parsing
    $xmlDoc->loadHTML("Your HTML Code
    "); //select the element that you want the attribute from...you may need to use $xmlDoc->getElementsByTagName('p'); $p_element = $xmlDoc->getElementById('yourtag'); //get the attribute alt of the selected element $alt = $p_element->getAttribute('alt'); //show alt attribute value echo $alt;

    This is just pseudo code and will not solve your problem, however it seems to be a better solution than the parser you are trying to use. Look at these links for more information (I hope this helps):

    http://www.php.net/manual/en/domdocument.construct.php

    http://php.net/manual/en/domelement.getattribute.php

    http://www.php.net/manual/en/domdocument.getelementsbytagname.php

    http://www.php.net/manual/en/domdocument.getelementbyid.php

    0 讨论(0)
提交回复
热议问题