PHP get external page content

泪湿孤枕 提交于 2019-12-12 18:09:01

问题


i get the html from another site with file_get_contens, my question is how can i get a specific tag value?

let's say i have:

<div id="global"><p class="paragraph">1800</p></div>

how can i get paragraph's value? thanks


回答1:


If the example is really that trivial you could just use a regular expression. For generic HTML parsing though, PHP has DOM support:

$dom = new domDocument();
$dom->loadHTML("<div id=\"global\"><p class=\"paragraph\">1800</p></div>");
echo $dom->getElementsByTagName('p')->item(0)->nodeValue;



回答2:


You need to parse the HTML. There are several ways to do this, including using PHP's XML parsing functions.

However, if it is just a simple value (as you asked above) I would use the following simple code:

// your content
$contents='<div id="global"><p class="paragraph">1800</p></div>';

// define start and end position
$start='<div id="global"><p class="paragraph">';
$end='</p></div>';

// find the stuff
$contents=substr($contents,strpos($contents,$start)+strlen($start));
$contents=substr($contents,0,strpos($contents,$end));

// write output
echo $contents;

Best of luck!

Christian Sciberras

(tested and works)




回答3:


$input  = '<div id="global"><p class="paragraph">1800</p></div>';
$output = strip_tags($input);



回答4:


preg_match_all('#paragraph">(.*?)<#is', $input, $output);

print_r($output);

Untested.



来源:https://stackoverflow.com/questions/2691798/php-get-external-page-content

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