开发者

extract text from tag

开发者 https://www.devze.com 2022-12-09 19:40 出处:网络
Hi I\'ve got these lines here, I am trying to extract the first paragraph found in the file, but this fails to return any results, if not it returns results that are not even in <p> tags which i

Hi I've got these lines here, I am trying to extract the first paragraph found in the file, but this fails to return any results, if not it returns results that are not even in <p> tags which is odd?

$file = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];
$hd = fopen($file,'r');
$cn = fread($hd, filesize($file));
fclose($hd);

$cnc = preg_replace('/<p>(.+?)&开发者_StackOverflowlt;\/p>/','$1',$cn);


Try this:

$html = file_get_contents("http://localhost/foo.php");
preg_match('/<p>(.*)<\/p>/', $html, $match);
echo($match[1]);


I would use DOM parsing for that:

// SimpleHtmlDom example
// Create DOM from URL or file
$html = file_get_html('http://localhost/blah.php');

// Find all paragraphs 
foreach($html->find('p') as $element) 
       echo $element->innerText() . '<br>';

It would allow you to more reliably replace some of the markup:

$html->find('p', 0)->innertext() = 'foo';
0

精彩评论

暂无评论...
验证码 换一张
取 消