Need help with a Loop

Discussion in 'PHP' started by stangparts, Feb 8, 2009.

  1. #1
    I am using this code to retrieve text from a url, what I am having trouble with is I need to loop through the page and return each instance of the text requested.

    I would appreciate some help.


    function between($start, $end, $source) {
            $s = strpos($source, $start) + strlen($start);
            return substr($source, $s, strpos($source, $end, $s) - $s);
    }
    
    $data = file_get_contents('http://www.url-here.com/');
    $string = between('<td class="table">', '</td>', $data);
    Code (markup):

     
    stangparts, Feb 8, 2009 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    function between($start, $end, $source) {
    $string = '';
    while( $s = strpos($source, $start) + strlen($start))
    {
    $string .= substr($source, $s, strpos($source, $end, $s) - $s) . "<br>";
    }
    return $string;
    }
     
    bartolay13, Feb 8, 2009 IP
  3. stangparts

    stangparts Member

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    33
    #3
    I appreciate the response...I however couldn't get it to work. I did get this code to work, the only problem is I need it in an array. I need to be able to refer to each instance it finds with like 'echo $data[5];' or something?

    I would greatly appreciate the help as I am in a pinch.

    $fp = file_get_contents('http://www.url-here.com/');
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
    } else {
    	$out = "GET / HTTP/1.1\r\n";
    	$out .= "Host: www.yahoo.com\r\n";
    	$out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12\r\n";
    	$out .= "Connection: Close\r\n\r\n";
     
    	$str = "";
      
    	
    		$str .= $fp;
    	}
    	
    
     
    $pos = strpos($str, "<table id=\"idhere\"");
    $pos = $pos + strlen("<table id=\"idhere\"");
    
    if($pos == false) {
    	echo "No information available";
    }
    else {
     
    	while(1) {
    		$pos = strpos($str, "<td class=\"classhere\">", $pos);
     
    		if($pos === false) {
    			break;
    		}
     
    		$pos = $pos + strlen("<td class=\"classhere\">");
    		$temppos = $pos;
    		$pos = strpos($str, "</td>", $pos);
     
    		$datalength = $pos - $temppos;
     
    		$data = substr($str, $temppos , $datalength);
    		
    	}
     
    }
    Code (markup):
     
    stangparts, Feb 11, 2009 IP
  4. Singhals

    Singhals Banned

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    untested plz test it before use...
     
    Singhals, Feb 12, 2009 IP