Jump to content

[SOLVED] preg_match_all


Modernvox

Recommended Posts

Would like to know if i am using the correct regex to return a link similar to [email protected]

Problem is i keep getting a Parse error: syntax error, unexpected $end in C:\xampp\htdocs\test4.php on line 45?

 

    function curlURL($url) {  
        $curl = curl_init();  
        curl_setopt($curl, CURLOPT_URL, $url);  
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2');  
        $output = curl_exec($curl);  
            return $output;  
    }  
     
   $curlResults = curlURL("http://southcoast.craigslist.org/sss/");  
   $pattern = '#<a href="(/[a-z]{3}/\d{10}\.html)">#';
   preg_match_all( $pattern, $curlResults, $matches);

echo "<pre>\n";
echo "Links:\n\n";
foreach ($matches[1] as $link):
   echo "\t" . '<a href="' . $link . '" target="_BLANK">' . $link . '</a>' . "\n";
endforeach;
echo '</pre>';


echo file_get_contents("http://southcoast.craigslist.org".$link);
$pattern = '#<sale-([a-z0-9]?/\d{10}\.html)">#';  //This is the attempted match for the email
preg_match_all( $pattern, $matches);

foreach ($matches[1] as $address): 

$dbx= mysql_connect("localhost", "peter", "");   //include before any database implematation
if (!$dbx)
{
die('Could not connect: ' . mysql_error());
}

mysql_SELECT_db("Craigslist", $dbx);
mysql_Query("INSERT INTO addresses (sale_items)
VALUES ('$address');

mysql_close($dbx);

?> 

Link to comment
https://forums.phpfreaks.com/topic/179695-solved-preg_match_all/
Share on other sites

Why does that do nothing for you? Your missing an endforeach;

 

I added the endforeach; and the results were the same.

I did however add the following and got it to work, but now i am trying to open on =e of them with no luck yet. Error states 2 parameters required..

 

 <?php  
    function curlURL($url) {  
        $curl = curl_init();  
        curl_setopt($curl, CURLOPT_URL, $url);  
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2');  
        $output = curl_exec($curl);  
            return $output;  
    }  
     
   $curlResults = curlURL("http://southcoast.craigslist.org/sss/");  
   $pattern = '#<a href="(/[a-z]{3}/\d{10}\.html)">#';
   preg_match_all( $pattern, $curlResults, $matches);

echo "<pre>\n";
echo "Links:\n\n";
foreach ($matches[1] as $link):
   echo "\t" . '<a href="' . $link . '" target="_BLANK">' . $link . '</a>' . "\n";
endforeach;
echo '</pre>';


echo file_get_contents("http://southcoast.craigslist.org".$link);
$pattern = '#<sale-([a-z0-9]?/\d{10}\.html)">#';
preg_match( $pattern, $matches);
print_r ($matches[0]); //Have 99 available links to click
$fp= fopen($matches[2]); //How can i choose one and open it?

?> 

but now i am trying to open on =e of them with no luck yet

 

Sorry, but that makes no sense at all from here.

 

Error states 2 parameters required..

 

In regard to what function?

 

Yeah, I am getting lost, sorry.

I mean to say. I added the endforeach with no luck. What i have on my screen now is the active links list and ONE open link. I am attempting to copy the email address from the ONE open link using the preg_match function #<sale-([a-z0-9]?/\d{10}\.html)">#' so I may send to DB. I just keep getting errors when i attempt to copy the email whether i use foreach, if etc.. I just don't have enough experience to produce the proper code.

 

I hope I sound a little more clear. Happy Halloween :P

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.