Simple array to file problem

Discussion in 'PHP' started by xchris, Jul 11, 2008.

  1. #1
    I am having some trouble with the php script i'm writing (php newbie). The problem is that i have to write a preg_match_all array to a text file, and i need your help. Forgot to mention that the script that i'm writing is a alltheweb search results link scraper. Thanks in advance

    Here is the code:

    <?php
    // get the HTML
    
    $target_url = "http://alltheweb.com/search?advanced=1&cat=web&jsact=&_stype=norm&type=all&q=site%3Ayahoo.com&itag=crv&l=en&ics=utf-8&cs=iso88591&wf[n]=3&wf[0][r]=%2B&wf[0][q]=&wf[0][w]=&wf[1][r]=%2B&wf[1][q]=&wf[1][w]=&wf[2][r]=-&wf[2][q]=&wf[2][w]=&dincl=&dexcl=&geo=&doctype=&dfr[d]=1&dfr[m]=1&dfr[y]=1980&dto[d]=9&dto[m]=7&dto[y]=2008&hits=100";
    $userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    curl_setopt($ch, CURLOPT_URL,$target_url);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    $html = curl_exec($ch);
    
    // find the links
    preg_match_all ('/<span class="resTitle"><a class="res" href="(.*?)" >(.*?)</a></span>/s', $html, $matches,PREG_SET_ORDER);
    
    // print the links to the screen
    foreach ($matches as $above) {
    
    	$hyper = $above[0];
    	echo $hyper;
    	echo "<br/>";
    	$scrlink = $above[1];
    	echo $scrlink;
    	echo "<br/>";
    	$ancortexttext = $above[2];
    	echo $ancortexttext;
    	echo "<br/>";
    
    	}
    
    ?>
    Code (markup):

     
    xchris, Jul 11, 2008 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Your regex and your code were both a bit messy, click the download link on the page below to see working (and readable) code.

    http://danltn.com/10
     
    Danltn, Jul 11, 2008 IP
    xchris likes this.
  3. xchris

    xchris Peon

    Messages:
    111
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you! Rep given!
     
    xchris, Jul 11, 2008 IP