jk11uk Posted February 6, 2008 Share Posted February 6, 2008 Hi i have a php script that takes a url and loads all the code from that page into a variable. I then want to scan through that variable and pick out the first 5 URLs. So i would need something which looks for the first 5 times that <a href="http://........someurl....."> happens and store the occurances into different variables. can anyone work it out? i'm very stuck ??? Quote Link to comment https://forums.phpfreaks.com/topic/89721-php-string-searching/ Share on other sites More sharing options...
cooldude832 Posted February 6, 2008 Share Posted February 6, 2008 file_get_contents then regex or strip tags without anchors <?php $file = ""; $data = file_get_content($file); $data = strip_tags($data, "<a>"); $data = explode("<a",$data); print_r($data); Quote Link to comment https://forums.phpfreaks.com/topic/89721-php-string-searching/#findComment-459739 Share on other sites More sharing options...
aschk Posted February 6, 2008 Share Posted February 6, 2008 Explode on <a will give you EVERYTHING before and after the next tag, which will include content from the page too..., which is NOT the result you are after. Use a regular expression instead. Quote Link to comment https://forums.phpfreaks.com/topic/89721-php-string-searching/#findComment-459744 Share on other sites More sharing options...
cooldude832 Posted February 6, 2008 Share Posted February 6, 2008 it will explode each anchor so yes they will all need trimmming, but for those of us without regex this works out Quote Link to comment https://forums.phpfreaks.com/topic/89721-php-string-searching/#findComment-459746 Share on other sites More sharing options...
jk11uk Posted February 6, 2008 Author Share Posted February 6, 2008 i dont understand how that would work. so it strips the data of the <a and </a> tags. but then the explode would not find any <a things to divide the string by, as you have stripped them? Does anyone know the regex code that would do it? Quote Link to comment https://forums.phpfreaks.com/topic/89721-php-string-searching/#findComment-459755 Share on other sites More sharing options...
jk11uk Posted February 6, 2008 Author Share Posted February 6, 2008 anyone? please! Quote Link to comment https://forums.phpfreaks.com/topic/89721-php-string-searching/#findComment-459965 Share on other sites More sharing options...
effigy Posted February 6, 2008 Share Posted February 6, 2008 <pre> <?php $data = <<<DATA <a href="http://www.one.com">1</a> <a href="http://www.two.com">2</a> <a href="http://www.three.com">3</a> <a href="http://www.four.com">4</a> <a href="http://www.five.com">5</a> <a href="http://www.six.com">6</a> DATA; preg_match_all('%<a\s.+?</a>%s', $data, $matches); array_splice($matches[0], 5); print_r($matches); ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/89721-php-string-searching/#findComment-459973 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.