jskarr1982 Posted March 22, 2011 Share Posted March 22, 2011 Hello, My code: <?php $file = file_get_contents("https://www.amark.com/spotpricesst.asp"); $do = preg_match('/<a href="" onClick="NewWindow2('/includes/viewproduct.asp?ID=10','SpotPrice_INFO',610,300,'0','yes','center'); return false;">1 oz<\/a><\/nobr><\/td><\/tr><\/table><\/td><td class="spotsborderbottom"><table cellspacing=0 cellpadding=0 border=0><tr><td width=100%><\/td><td class="spotsnoborder">(.*)<\/td>/', $file, $matches); if ($do) { print $matches['1']; } else { echo "No Price"; } ?> Is to pull the price of gold from this website, however the coding I am using includes an =. Is there a way to go around this? I receive the following error: Parse error: syntax error, unexpected '=' in /home1/northes3/public_html/crawl/index.php on line 3 Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/231418-parse-error-syntax-error-unexpected-in/ Share on other sites More sharing options...
ManiacDan Posted March 22, 2011 Share Posted March 22, 2011 Look at your code. See how the coloring is wrong? That's because your string is wrong. If you use single-quotes ( ' ) to enclose a string, any single quotes inside the string must be escaped: ( \' ) -Dan Quote Link to comment https://forums.phpfreaks.com/topic/231418-parse-error-syntax-error-unexpected-in/#findComment-1190957 Share on other sites More sharing options...
jskarr1982 Posted March 22, 2011 Author Share Posted March 22, 2011 Dan, I am a beginner in this so I thank you for letting me know about that. My new code is now: <?php $file = file_get_contents("https://www.amark.com/spotpricesst.asp"); $do = preg_match('/<a href="" onClick="NewWindow2(\'/includes/viewproduct.asp?ID=10\',\'SpotPrice_INFO\',610,300,\'0\',\'yes\',\'center\'); return false;">1 oz<\/a><\/nobr><\/td><\/tr><\/table><\/td><td class="spotsborderbottom"><table cellspacing=0 cellpadding=0 border=0><tr><td width=100%><\/td><td class="spotsnoborder">(.*)<\/td>/', $file, $matches); if ($do) { print $matches['1']; } else { echo "No Price"; } ?> However I get: Warning: preg_match() [function.preg-match]: Unknown modifier 'n' in /home1/northes3/public_html/crawl/index.php on line 3 No Price I am not sure what the "n" is that it refers to? Quote Link to comment https://forums.phpfreaks.com/topic/231418-parse-error-syntax-error-unexpected-in/#findComment-1190963 Share on other sites More sharing options...
jskarr1982 Posted March 22, 2011 Author Share Posted March 22, 2011 I figured out to \ before any /. Now the string: <?php $file = file_get_contents("https://www.amark.com/spotpricesst.asp"); $do = preg_match('/<nobr><a href="" onClick="NewWindow2(\'\/includes\/viewproduct.asp?ID=10\',\'SpotPrice_INFO\',610,300,\'0\',\'yes\',\'center\'); return false;">1 oz<\/a><\/nobr><\/td><\/tr><\/table><\/td><td class="spotsborderbottom"><table cellspacing=0 cellpadding=0 border=0><tr><td width=100%><\/td><td class="spotsnoborder">(.*)<\/td>/', $file, $matches); if ($do) { print $matches['1']; } else { echo "No Price"; } ?> Simply doesn't find the target code. Do you think it's just too buggy? Quote Link to comment https://forums.phpfreaks.com/topic/231418-parse-error-syntax-error-unexpected-in/#findComment-1190977 Share on other sites More sharing options...
jskarr1982 Posted March 22, 2011 Author Share Posted March 22, 2011 I am trying to pull content from an ASP file if that makes any difference? Quote Link to comment https://forums.phpfreaks.com/topic/231418-parse-error-syntax-error-unexpected-in/#findComment-1190992 Share on other sites More sharing options...
ManiacDan Posted March 23, 2011 Share Posted March 23, 2011 There are special characters in regular expressions too. These characters must be escaped with a back slash: /?*$^. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/231418-parse-error-syntax-error-unexpected-in/#findComment-1191290 Share on other sites More sharing options...
jskarr1982 Posted March 23, 2011 Author Share Posted March 23, 2011 Dan, Thank you so much! It works now, however it doesn't stop at the ending </td>. It keeps on going. Does PHP pick the first </td> or is that not specific enough for it to know when to stop? It pulls up the whole page after the price that I want to grab. My example is at: http://www.northernnightsweb.com/crawl Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/231418-parse-error-syntax-error-unexpected-in/#findComment-1191458 Share on other sites More sharing options...
ManiacDan Posted March 24, 2011 Share Posted March 24, 2011 (.*) <-- that selects as many things as it can, as long as the whole expression is still valid. Regular expressions are greedy, and will match things as wide as it can. You must use the "ungreedy" modifier to prevent this. Change (.*) to (.*?) -Dan Quote Link to comment https://forums.phpfreaks.com/topic/231418-parse-error-syntax-error-unexpected-in/#findComment-1191715 Share on other sites More sharing options...
jskarr1982 Posted March 24, 2011 Author Share Posted March 24, 2011 Excellent!! That solved the problem. Thank you so much for teaching me how to properly do this! Quote Link to comment https://forums.phpfreaks.com/topic/231418-parse-error-syntax-error-unexpected-in/#findComment-1191775 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.