daveh33 Posted October 12, 2009 Share Posted October 12, 2009 Hi, I am trying to write a script for my website to get an article from an external website. The external website has the article in a set of tags <roottag> & </roottag> The code I am using is $page = file_get_contents($url); preg_match_all("/<roottag>.+<\/roottag>/", $page, $matches); print_r($matches); when this code runs the below is output: - Array ( [0] => Array ( ) ) I've not used this function in php and am a bit stuck! so any help is really appreciated Quote Link to comment https://forums.phpfreaks.com/topic/177454-solved-reading-partial-code-from-external-site/ Share on other sites More sharing options...
lemmin Posted October 12, 2009 Share Posted October 12, 2009 Try like this: preg_match_all("/<roottag>(.+?)<\/roottag>/", $page, $matches); Quote Link to comment https://forums.phpfreaks.com/topic/177454-solved-reading-partial-code-from-external-site/#findComment-935652 Share on other sites More sharing options...
daveh33 Posted October 12, 2009 Author Share Posted October 12, 2009 Thanks - I have changed that line of code and the output is now: - Array ( [0] => Array ( ) [1] => Array ( ) ) Quote Link to comment https://forums.phpfreaks.com/topic/177454-solved-reading-partial-code-from-external-site/#findComment-935666 Share on other sites More sharing options...
PFMaBiSmAd Posted October 12, 2009 Share Posted October 12, 2009 Either $page is empty or it does not contain the <roottag> </roottag> tags. Have you echoed $page to see exactly what is in it? Quote Link to comment https://forums.phpfreaks.com/topic/177454-solved-reading-partial-code-from-external-site/#findComment-935671 Share on other sites More sharing options...
daveh33 Posted October 12, 2009 Author Share Posted October 12, 2009 yes - when I echo $page it loads the entire webpage where the article is. When I view the source I can see the <roottag> </roottag> tags Quote Link to comment https://forums.phpfreaks.com/topic/177454-solved-reading-partial-code-from-external-site/#findComment-935677 Share on other sites More sharing options...
lemmin Posted October 12, 2009 Share Posted October 12, 2009 Can you post the code, then? I think PFMaBiSmAd is correct in that your input is not what you expect because the regex should work. Quote Link to comment https://forums.phpfreaks.com/topic/177454-solved-reading-partial-code-from-external-site/#findComment-935681 Share on other sites More sharing options...
daveh33 Posted October 12, 2009 Author Share Posted October 12, 2009 <? $url = "http://www.thesun.co.uk/sol/homepage/sport/other_sports/racing/1142207/Templegates-racing-tips.html"; $page = file_get_contents($url); // echo $page; preg_match_all("/<roottag>(.+?)<\/roottag>/", $page, $matches); print_r($matches); ?> Quote Link to comment https://forums.phpfreaks.com/topic/177454-solved-reading-partial-code-from-external-site/#findComment-935684 Share on other sites More sharing options...
lemmin Posted October 12, 2009 Share Posted October 12, 2009 Oh sorry, it is because of the new lines. Put the 's' modifier into your regex: preg_match_all("/<roottag>(.+?)<\/roottag>/s", $page, $matches); Quote Link to comment https://forums.phpfreaks.com/topic/177454-solved-reading-partial-code-from-external-site/#findComment-935690 Share on other sites More sharing options...
Alex Posted October 12, 2009 Share Posted October 12, 2009 try: <?php $url = "http://www.thesun.co.uk/sol/homepage/sport/other_sports/racing/1142207/Templegates-racing-tips.html"; $page = file_get_contents($url); preg_match_all("~\<roottag\>(.+?)\<\/roottag\>~s", $page, $matches); print_r($matches); ?> edit: Late Quote Link to comment https://forums.phpfreaks.com/topic/177454-solved-reading-partial-code-from-external-site/#findComment-935691 Share on other sites More sharing options...
daveh33 Posted October 12, 2009 Author Share Posted October 12, 2009 :-) I can see the article! But it still has 'Array ( [0] => Array ( [0] =>' at the start, it's also looping and showing it twice... Quote Link to comment https://forums.phpfreaks.com/topic/177454-solved-reading-partial-code-from-external-site/#findComment-935696 Share on other sites More sharing options...
lemmin Posted October 12, 2009 Share Posted October 12, 2009 That is because $matches in an array. The first value in the array is the full string that it matched and the second is the string inside of the parentheses in the regex. To use the string, you can just echo $matches[1]. Quote Link to comment https://forums.phpfreaks.com/topic/177454-solved-reading-partial-code-from-external-site/#findComment-935700 Share on other sites More sharing options...
daveh33 Posted October 12, 2009 Author Share Posted October 12, 2009 Thats what I thought, I've changed the echo/print // print_r($matches); echo $matches[1]; but it just displays 'Array' Quote Link to comment https://forums.phpfreaks.com/topic/177454-solved-reading-partial-code-from-external-site/#findComment-935702 Share on other sites More sharing options...
daveh33 Posted October 12, 2009 Author Share Posted October 12, 2009 ....I have changed the function to preg_match instead of preg_match_all and its working perfectly thank you so much for your help everyone! much appreciated!!! Quote Link to comment https://forums.phpfreaks.com/topic/177454-solved-reading-partial-code-from-external-site/#findComment-935703 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.