cuffy Posted April 1, 2009 Share Posted April 1, 2009 I hope someone can help me. I have searched for ages for a solution and cannot find a simple answer anywhere. There are many scripts that will extract and display all the links from a single web-page but what I am looking for is a script that will output only part of the link. For example if one of the array outputs was www.bbc.co.uk/football/1234 then all I want is 1234 to be the output and to ignore all the other parts of the url or any other url which does not start with www.bbc.co.uk/football I hope someone can help - it's driving me mad. Quote Link to comment https://forums.phpfreaks.com/topic/152151-solved-extracting-part-of-link-from-url/ Share on other sites More sharing options...
JD* Posted April 1, 2009 Share Posted April 1, 2009 Do you have a defined area you want to get (ie always the last part, always after the 3rd slash, etc)? If so, just break out into an array based on the '/' and use your different array functions to get that part. If not, post back on here and let us know. As for the second part, you can do string matching with strpos Quote Link to comment https://forums.phpfreaks.com/topic/152151-solved-extracting-part-of-link-from-url/#findComment-799072 Share on other sites More sharing options...
teng84 Posted April 1, 2009 Share Posted April 1, 2009 $string = 'www.bbc.co.uk/football/1234 '; preg_match_all('#www\.bbc\.co\.uk\/football\/([\d]+)#mis',$string,$match); print_r($match); this? Quote Link to comment https://forums.phpfreaks.com/topic/152151-solved-extracting-part-of-link-from-url/#findComment-799078 Share on other sites More sharing options...
cuffy Posted April 2, 2009 Author Share Posted April 2, 2009 Thank you for your amazing quick response. I am sorry but my ability to code php is fairly limited but I am trying ! The link always follows the following format; ie: "http://cgi1.igl.net/tourney/ydom/196831". The original url where I was looking to extract the link from is found at: "http://www4.igl.net/cgi-bin/tourney/gencalendar.cgi?path=ydom" I am merely looking for a function/array that will produce the numeric value of the link. In the above example "196831". I was hoping that I could set a variable of something like $tourney where I can reuse the $tourney[0] is some html that I am writing. Quote Link to comment https://forums.phpfreaks.com/topic/152151-solved-extracting-part-of-link-from-url/#findComment-799084 Share on other sites More sharing options...
JD* Posted April 2, 2009 Share Posted April 2, 2009 So I think it would be something like this: $tourney = explode('/', url here, however you're getting it) echo $tourney[array_size($tourney)-1]; That should give you the last value which is your number. Unfortunately, I can't test right now, but hopefully it gets you moving in the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/152151-solved-extracting-part-of-link-from-url/#findComment-799090 Share on other sites More sharing options...
cuffy Posted April 2, 2009 Author Share Posted April 2, 2009 Oh and to make it clear I was looking to extract all the occurances. For example if there are 3 results I would like: $tourney[0] to be one result $tourney[1] to be the next and so forth Quote Link to comment https://forums.phpfreaks.com/topic/152151-solved-extracting-part-of-link-from-url/#findComment-799091 Share on other sites More sharing options...
Andy-H Posted April 2, 2009 Share Posted April 2, 2009 Look into http://php.net/parse_url and http://php.net/split Quote Link to comment https://forums.phpfreaks.com/topic/152151-solved-extracting-part-of-link-from-url/#findComment-799102 Share on other sites More sharing options...
cuffy Posted April 2, 2009 Author Share Posted April 2, 2009 Thank you for your pointers. Whilst I am sure you have better methods and I haven't got my variables I am sure I have something that I'll be able work around: <?PHP $page = 0; $URL = "http://www4.igl.net/cgi-bin/tourney/gencalendar.cgi?path=ydom"; $page = @FOPEN($URL, "r"); WHILE(!FEOF($page)) { $line = FGETS($page, 255); WHILE(EREGI("HREF=\"[^\"]*\"", $line, $match)) { $newstring = substr($match[0], 39, 6); echo $newstring; PRINT("<BR>\n"); $replace = EREG_REPLACE("\?", "\?", $match[0]); $line = EREG_REPLACE($replace, "", $line); } } FCLOSE($page); ?> Quote Link to comment https://forums.phpfreaks.com/topic/152151-solved-extracting-part-of-link-from-url/#findComment-799125 Share on other sites More sharing options...
Salkcin Posted April 2, 2009 Share Posted April 2, 2009 use file_get_contents() to save the contents from the page in a variable, then extract the info you want using regular expressions such as preg_match_all(). here's a snippet that will fetch the page you want, extract the values and print them out on the screen, ex: <?php $html = file_get_contents('http://www4.igl.net/cgi-bin/tourney/gencalendar.cgi?path=ydom'); preg_match_all('/(?<=Jump: )\d+/s', $html, $matches); foreach($matches[0] as $value) { echo "$value<br />"; } ?> and the output will look something like this: 196791 196831 196904 196918 196929 Quote Link to comment https://forums.phpfreaks.com/topic/152151-solved-extracting-part-of-link-from-url/#findComment-799185 Share on other sites More sharing options...
cuffy Posted April 2, 2009 Author Share Posted April 2, 2009 Thanks for all your help - it has been invaluable. I have managed to extract the times for the page but for some reason I just cannot extract the text after "Location: ". I know I am going wrong with the expression in the preg_match_all function but I just cannot see where. What I have is: $html = file_get_contents('http://www4.igl.net/cgi-bin/tourney/gencalendar.cgi?path=ydom'); preg_match_all("/Location:(.*)<\/br>/", $html, $matches); but I seem to be getting no results. Quote Link to comment https://forums.phpfreaks.com/topic/152151-solved-extracting-part-of-link-from-url/#findComment-799516 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.