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

蹲街弑〆低调 提交于 2019-12-01 19:56:38

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("<html><body>Your HTML Code<br></body></html>");
//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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!