rule69 Posted April 26, 2008 Share Posted April 26, 2008 Hi i want to do this but i dunno where to start havent really looked into regex.. so maybe some can help me out.. Lets Say i post a link like this from a form: http://yourdomain.com/Dj_Mtamerr_-_Lessons_In_Love_-[32kbs]-_[YourDomain.com].mp3 what i need to getin return is this: Artist: Dj Mtamerr Track: Lessons In Love AND IN Cases like: http://yourdomain.com/Dj_Mtamerr_Ft_Dj_Sdoko_-_Lost_In_Time_-[32kbs]-_[YourDomain.com].mp3 Artist: Dj Mtamerr Ft. Dj Sdoko Track: Lessons In Love to be printed out dunno if its possible bu if it is some1 please show me the way ty. Quote Link to comment https://forums.phpfreaks.com/topic/103003-extract-text-from-link/ Share on other sites More sharing options...
npsari Posted April 26, 2008 Share Posted April 26, 2008 $get_url = $_SERVER['REQUEST_URI']; $link1 = $get_url; $link2 = substr("$link1", 7, -1); $link3 = strtok($link2, "/"); print "link3"; U need to play with these two substr() strtok() Quote Link to comment https://forums.phpfreaks.com/topic/103003-extract-text-from-link/#findComment-527644 Share on other sites More sharing options...
Daniel0 Posted April 26, 2008 Share Posted April 26, 2008 <?php $url = 'http://yourdomain.com/Dj_Mtamerr_-_Lessons_In_Love_-[32kbs]-_[YourDomain.com].mp3'; list($artist, $track) = explode('-', parse_url($url, PHP_URL_PATH)); $artist = str_replace('_', ' ', ltrim($artist, '/')); $track = str_replace('_', ' ', $track); ?> Try that. It isn't tested. Edit: Just tested it. It does work. You might want to run both $artist and $track through trim() to remove the spaces at the start and end of the string. Quote Link to comment https://forums.phpfreaks.com/topic/103003-extract-text-from-link/#findComment-527645 Share on other sites More sharing options...
rule69 Posted April 26, 2008 Author Share Posted April 26, 2008 Thanks for both post especially Daniel0 your code worked wonders EDIT: well just tested my self all is good until i use a link like: http://yourdomain.com/files/1186885_f5plf/Dj_Mtamerr_-_Lessons_In_Love_-[32kbs]-_[YourDomain.com].mp3 now it prints Artist:files/1186885 f5plf/Dj Mtamerr Track: Lessons In Love is there a way to exclude any text before the "/" anyways i thnk i need a new phpbook any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/103003-extract-text-from-link/#findComment-527648 Share on other sites More sharing options...
Daniel0 Posted April 26, 2008 Share Posted April 26, 2008 Ah, well... I just assumed the domain would look sort of like the one you originally posted. Try this instead: <?php $url = 'http://yourdomain.com/files/1186885_f5plf/Dj_Mtamerr_-_Lessons_In_Love_-[32kbs]-_[YourDomain.com].mp3'; list($artist, $track) = explode('-', strrchr($url, '/')); $artist = trim(str_replace('_', ' ', ltrim($artist, '/'))); $track = trim(str_replace('_', ' ', $track)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/103003-extract-text-from-link/#findComment-527652 Share on other sites More sharing options...
rule69 Posted April 26, 2008 Author Share Posted April 26, 2008 Thanks again great work mate props to u man Quote Link to comment https://forums.phpfreaks.com/topic/103003-extract-text-from-link/#findComment-527653 Share on other sites More sharing options...
rule69 Posted April 26, 2008 Author Share Posted April 26, 2008 can u please look at this and tell me why the data is not getting printed? im posting it from a form the artist and the track strings are blank the rest of the data posts ??? <?php for($i=0;$i<$forms;$i++){ $url = '$link[$i]'; list($artist, $track) = explode('-', strrchr($url, '/')); $artist = str_replace('_', ' ', ltrim($artist, '/')); $track = str_replace('_', ' ', $track); echo "<table> <tr><td class=\"lefts\" style=\"text-align: left;\"> Name: <b>$track[$i]</b> </td><td class=\"right\"></tr> <tr><td class=\"lefts\" style=\"text-align: left;\"> Artist: <b>$artist[$i]</b> </td><td class=\"right\"></tr> <tr><td class=\"lefts\" style=\"text-align: left;\"> Link: <b>$link[$i]</b> </td><td class=\"right\"></tr> <tr><td class=\"lefts\" style=\"text-align: left;\"> Cat: <b>$cat[$i]</b> </td><td class=\"right\"></tr> <tr><td class=\"lefts\" style=\"text-align: left;\"> Alpha: <b>$alpha[$i]</b </td><td class=\"right\"></tr> </table> has been added successfully"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/103003-extract-text-from-link/#findComment-527662 Share on other sites More sharing options...
Daniel0 Posted April 26, 2008 Share Posted April 26, 2008 Change Name: <b>$track[$i]</b> to Name: <b>$track</b> and change Artist: <b>$artist[$i]</b> to Artist: <b>$artist</b> Neither of those two variables are arrays. Quote Link to comment https://forums.phpfreaks.com/topic/103003-extract-text-from-link/#findComment-527664 Share on other sites More sharing options...
rule69 Posted April 26, 2008 Author Share Posted April 26, 2008 done that but the returned text is still blank Name: Artist: Link: http://yourdomain.com/files/1186885_f5plf/Dj_Mtamerr_-_Lessons_In_Love_-[32kbs]-_[YourDomain.com].mp3 Cat: rnb Alpha: d has been added successfully Name: Artist: Link: http://yourdomain.com/files/1186885_f5plf/Dj_Mtamerr_-_Lessons_In_Love_-[32kbs]-_[YourDomain.com].mp3 Cat: rnb Alpha: o has been added successfully Quote Link to comment https://forums.phpfreaks.com/topic/103003-extract-text-from-link/#findComment-527665 Share on other sites More sharing options...
Daniel0 Posted April 26, 2008 Share Posted April 26, 2008 Ah... you also need to change $url = '$link[$i]'; to $url = $link[$i]; Quote Link to comment https://forums.phpfreaks.com/topic/103003-extract-text-from-link/#findComment-527673 Share on other sites More sharing options...
rule69 Posted April 26, 2008 Author Share Posted April 26, 2008 tried that before didnt wok cus of those undefined variables btw it works now thank u again Quote Link to comment https://forums.phpfreaks.com/topic/103003-extract-text-from-link/#findComment-527699 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.