WEB Anyone online/ not busy? I need some help with some basic php shit.

Discussion in 'OT Technology' started by Ricky, Mar 2, 2009.

  1. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,766
    Likes Received:
    6
    Why isnt this shit working?

    Code:
    <?php
    
    $curl_handle=curl_init();
    curl_setopt($curl_handle,CURLOPT_URL,'http://www.website.com/lawl/');
    curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
    curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,true);
    $result = curl_exec($curl_handle);
    curl_close($curl_handle);
    
    if (empty($result))
    {
        print "Can't connect with curl";
    }
    else
    {
    	$replace = "Price";
    	$replacement = "This is what has been replaced";
    $done = echo preg_replace($replace, $replacement, $result);
    }
    ?>
    
     
  2. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,766
    Likes Received:
    6
    ultimately i want to use the preg replace to grab everything inside a div.

    the curl works perfectly without the preg match shit, but when i try to use the preg match it doesn't bring up anything.
     
  3. hank85

    hank85 sudo shred /dev/sda -f -v -z --iterations=6

    Joined:
    Jul 23, 2008
    Messages:
    4,360
    Likes Received:
    0
    Use str_replace instead of preg_replace.

    Preg is used for regular expressions while str_replace is for simple string replacements.
     
  4. hank85

    hank85 sudo shred /dev/sda -f -v -z --iterations=6

    Joined:
    Jul 23, 2008
    Messages:
    4,360
    Likes Received:
    0
    Also,

    Code:
    $done = echo preg_replace($replace, $replacement, $result);
    should be:

    Code:
    $done = str_replace($replace, $replacement, $result);
    print_r($done);
    Print_r will display the contents of $done without having to worry if it's an array or an object.
     
  5. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,766
    Likes Received:
    6
    thanks hank. i'll try that out now
     
  6. hank85

    hank85 sudo shred /dev/sda -f -v -z --iterations=6

    Joined:
    Jul 23, 2008
    Messages:
    4,360
    Likes Received:
    0

    My bad, I missed this.

    The target div:
    Code:
    <div id=".priceBox">$49.99</div>
    Capture the div:

    Code:
    $OpenTag = '<div id=".priceBox">';
    $CloseTag = '</div>;
    $result = explode($OpenTag,$result);
    
    At this point, everything before $OpenTag is in the array $result[0] and everything after is in $result[1]. $OpenTag itself is not included.

    Now we're going to get rid of </div> and everything after it leaving us only what's between the two tags:

    Code:
    $result = explode($CloseTag,$result[1]);
    $result = $result[0]; //Destroy the array and only put what between the tags into $result
    All of the above put together into a function:

    Code:
    function GrabInBetween($StartTag, $EndTag, $File){
         $Contents = explode($StartTag, $File);
         $Contents = explode($EndTag, $Contents[1]);
         return $Contents[0];
    }
    And to call it you would:

    Code:
    $OpenTag = '<div id=".priceBox">';
    $CloseTag = '</div>;
    $done = GrabInBetween($OpenTag, $CloseTag, $result);
     
    Last edited: Mar 2, 2009
  7. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,766
    Likes Received:
    6
    seems to be working. thanks a lot.

    im going to try and mess around with it, to see if i can grab the contents of a div.
     
  8. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,766
    Likes Received:
    6
    eek you did it for me :bowdown:

    cant thank you enough.
     
  9. hank85

    hank85 sudo shred /dev/sda -f -v -z --iterations=6

    Joined:
    Jul 23, 2008
    Messages:
    4,360
    Likes Received:
    0
    No worries, let me know if you need any more help.
     
  10. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,766
    Likes Received:
    6
    so what im looking at in total should be this, correct.

    Code:
    <?php
    
    $curl_handle=curl_init();
    curl_setopt($curl_handle,CURLOPT_URL,'http://www.site.com/test/');
    curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
    curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,true);
    $result = curl_exec($curl_handle);
    curl_close($curl_handle);
    
    if (empty($result))
    {
        print "Can't connect with curl";
    }
    else
    {
    	$OpenTag = '<div class="gbox-dotd-container">';
    	$CloseTag = '</div>';
    	$result = explode($OpenTag,$result);
    	$result = explode($CloseTag,$result[1]);
    	$result = $result[0]; //Destroy the array and only put what between the tags into $result
    	
    	function GrabInBetween($StartTag, $EndTag, $File){
    	     $Contents = explode($OpenTag, $File);
    	     $Contents = explode($CloseTag, $Contents[1]);
    	     return $Contents[0];
    	}
    	$OpenTag = '<div class="gbox-dotd-container">';
    	$CloseTag = '</div>';
    	$done = GrabInBetween($OpenTag, $CloseTag, $result);
    	print $done;
    }
    ?>
    
     
    Last edited: Mar 2, 2009
  11. hank85

    hank85 sudo shred /dev/sda -f -v -z --iterations=6

    Joined:
    Jul 23, 2008
    Messages:
    4,360
    Likes Received:
    0
    Code:
    <?php
    function GrabInBetween($StartTag, $EndTag, $File){
              $Contents = explode($StartTag, $File);
              $Contents = explode($EndTag, $Contents[1]);
              return $Contents[0];
         }
    
    $curl_handle=curl_init();
    curl_setopt($curl_handle,CURLOPT_URL,'[URL]http://www.site.com/test/[/URL]');
    curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2)  ;
    curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,tr  ue);
    $result = curl_exec($curl_handle);
    curl_close($curl_handle);
    
    if (empty($result)){
        print "Can't connect with curl";
    }else{
        $OpenTag = '<div class="gbox-dotd-container">';
        $CloseTag = '</div>';
        $done = GrabInBetween($OpenTag, $CloseTag, $result);
        print $done;
    }
    ?>                                                                              
     
    Last edited: Mar 2, 2009
  12. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,766
    Likes Received:
    6
    hmm ithink i tried putting the function above.

    let me try your example
     
  13. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,766
    Likes Received:
    6
    if the funtion goes on the top, where should i put

    Code:
    $OpenTag = '<div class="gbox-dotd-container">';
    	$CloseTag = '</div>';
    	$result = explode($OpenTag,$result);
    	$result = explode($CloseTag,$result[1]);
    	$result = $result[0]; //Destroy the array and only put what between the tags into $result
    	
    
    im assuming it has to go after the curl init correct?

    hmm
     
  14. hank85

    hank85 sudo shred /dev/sda -f -v -z --iterations=6

    Joined:
    Jul 23, 2008
    Messages:
    4,360
    Likes Received:
    0
    You can remove all of that code.

    IM me if you have aim
     
  15. hank85

    hank85 sudo shred /dev/sda -f -v -z --iterations=6

    Joined:
    Jul 23, 2008
    Messages:
    4,360
    Likes Received:
    0
    Hey hold on. Variables got mixed up somewhere, I'm updating the code. give me one minute.
     
  16. hank85

    hank85 sudo shred /dev/sda -f -v -z --iterations=6

    Joined:
    Jul 23, 2008
    Messages:
    4,360
    Likes Received:
    0
    Edited in above posts as well for future OTers.


    Code:
    <?php
    function GrabInBetween($StartTag, $EndTag, $File){
              $Contents = explode($StartTag, $File);
              $Contents = explode($EndTag, $Contents[1]);
              return $Contents[0];
         }
    
    $curl_handle=curl_init();
    curl_setopt($curl_handle,CURLOPT_URL,'[URL]http://www.site.com/test/[/URL]');
    curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2)  ;
    curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,tr  ue);
    $result = curl_exec($curl_handle);
    curl_close($curl_handle);
    
    if (empty($result)){
        print "Can't connect with curl";
    }else{
        $OpenTag = '<div class="gbox-dotd-container">';
        $CloseTag = '</div>';
        $done = GrabInBetween($OpenTag, $CloseTag, $result);
        print $done;
    }
    ?>
     
  17. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,766
    Likes Received:
    6
    alright. thanks mister :bowdown:
     
  18. hank85

    hank85 sudo shred /dev/sda -f -v -z --iterations=6

    Joined:
    Jul 23, 2008
    Messages:
    4,360
    Likes Received:
    0
    Yeah, exactly.

    Let me know if it works for you.
     
  19. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,766
    Likes Received:
    6
    i think you might of made a mistake. i think i got it brb
     
  20. hank85

    hank85 sudo shred /dev/sda -f -v -z --iterations=6

    Joined:
    Jul 23, 2008
    Messages:
    4,360
    Likes Received:
    0
    I wouldn't doubt it, I can't see straight anymore.
     
  21. hank85

    hank85 sudo shred /dev/sda -f -v -z --iterations=6

    Joined:
    Jul 23, 2008
    Messages:
    4,360
    Likes Received:
    0
    I'll be back online tomorrow and look over all the code if you don't have it working by then. Gonna go pass out. Good luck.
     
  22. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,766
    Likes Received:
    6
    yeah you had the variable starttag and endtag.

    then the variables opentag closetag.

    i switched those around and put this in

    Code:
    <?php
    function GrabInBetween($OpenTag, $CloseTag, $result){
              $Contents = explode($OpenTag, $result);
              $Contents = explode($CloseTag, $Contents[1]);
              return $Contents[0];
         }
    
    $curl_handle=curl_init();
    curl_setopt($curl_handle,CURLOPT_URL,'http://www.site.com/test/');
    curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2)  ;
    curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,true);
    $result = curl_exec($curl_handle);
    curl_close($curl_handle);
    
    if (empty($result)){
        print "Can't connect with curl";
    }else{
        $OpenTag = '<div class="gbox-dotd-container">';
        $CloseTag = '</div>';
        $done = GrabInBetween($OpenTag, $CloseTag, $result);
        print $done;
    }
    ?>
    
    and it's working perfectly.

    thanks a lot. really helps give me an understand of how it works.
     
  23. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,766
    Likes Received:
    6
    i've been also fucking around with explode and got this

    Code:
    	$lol = explode('<div class="gbox-dotd-container">', $result);
    	echo $lol[1]; // part1
    	echo $lol[2]; // part2
    	echo $lol[5]; // part5
    
    how is explode structured where i can put the end tag?

    I am only seeing the opening part where you can enter it in.

    or would i have to structure it like hank, inside a function with 2 explodes for 1 div ?
     
  24. drpepper

    drpepper Active Member

    Joined:
    Nov 13, 2006
    Messages:
    38,076
    Likes Received:
    2
    Location:
    San Antonio
    wow one of the more informative threads on here. thanks. dont delete ill come back to this for future reference.
     
  25. Pepsi1975

    Pepsi1975 Mod of the Year

    Joined:
    Jan 6, 2005
    Messages:
    47,590
    Likes Received:
    1
    Location:
    Detroit
    :werd:

    props to ricky and hank
     

Share This Page