tags:

views:

52

answers:

2

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>(.+?)<\/p>/','$1',$cn);
A: 

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';
karim79
+1  A: 

Try this:

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