extract text from tag

后端 未结 2 1111
予麋鹿
予麋鹿 2020-12-18 17:24

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 eve

相关标签:
2条回答
  • 2020-12-18 18:05

    Try this:

    $html = file_get_contents("http://localhost/foo.php");
    preg_match('/<p>(.*)<\/p>/', $html, $match);
    echo($match[1]);
    
    0 讨论(0)
  • 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 讨论(0)
提交回复
热议问题