Modernvox Posted October 25, 2009 Share Posted October 25, 2009 Cags, Salathe, Daniel helped me with this yesterday, but not sure i took it all in. I am attempting to open the URL and grab all the links within using preg_match_all The problem is Parse error: syntax error, unexpected T_ECHO in C:\xampp\xampp\htdocs\test4.php on line 13 <?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/"); preg_match_all #<a href="(/[a-z]{3}/\d{10}\.html)">#, $matches; echo ($matches); ?> I think i may be having a rather tough time grasping the [0][1] idea. I mean can't i just hold the exact matches? Do i need to add the 2 parameters? Sorry if i sound like a broken record. I started with C++ months ago and now trying my hand at php which to me seems a bit tougher to grasp. I really want to become great at php so i refuse to give up. I appreciate the help with this inquiry, but also would like your thoughts on the BEST way to learn php without becoming overwhelmed. I have read 2 php and mysql books. Maybe i should be spending more time farting around with client code? I know when learning C++ i spent most time playing with code and it seemed to do the trick, but with php i thought i could just jump in and the syntax would be familiar. Although it is somewhat similar it is also confusing as hell to me. Things like ".." and '...' seem to be different in php as are others. Anyhow you guys obviously got past the hurdles and i would like some encouragement/guidance as to what really helped you become the great helpers you are now. I would love to be able to answer others questions. That must feel great!! Quote Link to comment Share on other sites More sharing options...
abazoskib Posted October 25, 2009 Share Posted October 25, 2009 isnt preg match all a function? try adding parenthesis aka preg_match_all() Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted October 25, 2009 Share Posted October 25, 2009 preg_match_all() is a function (parenthesis needed), and will match every instance found. the results will be stored as an array in $matches (therefore, echo'ing $matches will not return your desired result). Quote Link to comment Share on other sites More sharing options...
Modernvox Posted October 25, 2009 Author Share Posted October 25, 2009 So what i am missing. I added $matches[0][1] ? Still returns Parse error: syntax error, unexpected T_ECHO in C:\xampp\xampp\htdocs\test4.php on line 13 Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 25, 2009 Share Posted October 25, 2009 When you call a function you have to add parenthesis after the function name, if the function takes parameters they need to be comma seperated and inside the parenthesis, like you have done with the curlURL function. So if I was calling preg_match_all I would do it like so: preg_match_all('#<a href="(/[a-z]{3}/\d{10}\.html)">#', $matches); Note, parameter 1 is a string, hence the quotes ('string'); Quote Link to comment Share on other sites More sharing options...
Modernvox Posted October 25, 2009 Author Share Posted October 25, 2009 When you call a function you have to add parenthesis after the function name, if the function takes parameters they need to be comma seperated and inside the parenthesis, like you have done with the curlURL function. So if I was calling preg_match_all I would do it like so: preg_match_all('#<a href="(/[a-z]{3}/\d{10}\.html)">#', $matches); Note, parameter 1 is a string, hence the quotes ('string'); Cool I got it to work with the following code. The only problem now is the links are being returned as text and not active links? <?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 "Links:\n"; foreach ($matches[1] as $link) { echo $link . "\n"; } ?> Quote Link to comment Share on other sites More sharing options...
Modernvox Posted October 25, 2009 Author Share Posted October 25, 2009 How can i display these vertically single file? Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 25, 2009 Share Posted October 25, 2009 You need to add the anchor tag if they are only returned as text. As for a vertical list, there are two options, use the preformatted tag or use the br tag. for example: 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>'; And if you dont wish to use pre, ad a <br >tag after each </a> tag. Quote Link to comment Share on other sites More sharing options...
Modernvox Posted October 25, 2009 Author Share Posted October 25, 2009 You need to add the anchor tag if they are only returned as text. As for a vertical list, there are two options, use the preformatted tag or use the br tag. for example: 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>'; And if you dont wish to use pre, ad a <br >tag after each </a> tag. That's it Thank You a bunch. All links in vertical format and active.. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 25, 2009 Share Posted October 25, 2009 No problem Quote Link to comment Share on other sites More sharing options...
abazoskib Posted October 25, 2009 Share Posted October 25, 2009 Modernvox, surprisingly the best resources I've found in learning PHP include my background with C++, the php manual online, and actually having tasks to accomplish. It's hard to learn the more complicated side of PHP without having specific goals you need to achieve. For example, someone may want to build a CMS, or another may want to build a web app, and each of these will bring about their own unique problems. PHP is so vast that you wouldnt be able to learn everything just by completing one assignment. My best advice would be to build yourself some tools you can use, ex. ftp client, email client, one of the mob wars type of games, etc etc. Just pick something that interests you/ you can use. You'll be more motivated to finish it. Quote Link to comment Share on other sites More sharing options...
Modernvox Posted October 25, 2009 Author Share Posted October 25, 2009 Modernvox, surprisingly the best resources I've found in learning PHP include my background with C++, the php manual online, and actually having tasks to accomplish. It's hard to learn the more complicated side of PHP without having specific goals you need to achieve. For example, someone may want to build a CMS, or another may want to build a web app, and each of these will bring about their own unique problems. PHP is so vast that you wouldnt be able to learn everything just by completing one assignment. My best advice would be to build yourself some tools you can use, ex. ftp client, email client, one of the mob wars type of games, etc etc. Just pick something that interests you/ you can use. You'll be more motivated to finish it. Yes, I agree with having tasks. This is the route i am going with. I run an auction website simlar to ebay. I think playing around with this locally will help as well as taking on other tasks like small interactive programs. Do you have any recommendations for games aimed at beginners? Thanks.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.