how to echo only selected area of html loaded by file_get_contents()

Discussion in 'PHP' started by demetron, Apr 15, 2011.

  1. #1
    what i am trying to do is to load a html file via file_get_contents() and echo only a selected area say <div id="something"></div>

    is it any way possible?
     
    demetron, Apr 15, 2011 IP
  2. Sepehr

    Sepehr Peon

    Messages:
    568
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Yes, you need to find where this selected area begins and ends using the strpos() function. then you can extract it using substr() and the 2 positions you retrieved in the last part.

    There are also other ways like using regex or XML/HTML/DOM related functions.
     
    Sepehr, Apr 15, 2011 IP
  3. demetron

    demetron Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    first of all thanks for your quick reply :)
    i already thought of the idea similar like that but didn't know about the the strpos() function so i was doing something like

    <?php
    $blogpost = file_get_contents("http://any_page.html");


    $area_start= 123545;
    $area_stop= 4526356;

    for($i=$area_start;$i<=$area_stop;$i++)
    {
    echo $blogpost[$i];
    }
    ?>

    i'll try to resolve it now and then rply back if i need more help thanks again.
     
    Last edited: Apr 15, 2011
    demetron, Apr 15, 2011 IP
  4. demetron

    demetron Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ok problem solved :D thanks a lot Sepehr
     
    demetron, Apr 15, 2011 IP
  5. EitanXOR

    EitanXOR Greenhorn

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    You can also consider using a regex, see php.net/preg_match and php.net/preg_match_all
     
    EitanXOR, Apr 16, 2011 IP