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):
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; }
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):