0

I want to output the third element from the same HTML element. I know how to output a div or span with an id or a class but I have no idea how to output a same HTML element. I thought it was something with p[1] but it doesn't work.

I know there is a lot of answered questions about it but it never explained how to output the same HTML element without a class or id.

   website  : http://localhost/

    <p>example</p>
    <p>example1</p> <!-- i want to take this one -->
    <p>example2</p>
    -------------------
    php script  :

    <?php $curl = curl_init('http://localhost/');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    $code3 = curl_exec($curl);
    curl_close($curl);
    $code = '/<p>(.*?)<\/p>/s';
    $code6= preg_match($code, $code3, $code4);
    echo $code4[1]
    ?>
    /* doesn't work ..

also php.net doesnt give a good example about it so i hope someone can help me here.
thanks advanced !

*/
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Parsing HTML with regular expressions is a one-way ticket to having a bad time. [Use DOM](http://php.net/manual/en/book.dom.php). – Sammitch Sep 03 '13 at 22:14

1 Answers1

0

Try:

<?php 
    $curl = curl_init('http://justpaste.it/8s5v');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    $content = curl_exec($curl);
    curl_close($curl);

    preg_match_all('/<p>(.+)<\/p>/im', $content, $result);

    print_r($result);
    print "Selected: " . $result[0][1] . "\n";
?>
b.lotus
  • 66
  • 6