OhTheNoes Posted July 13, 2011 Share Posted July 13, 2011 Hi, I have a variable that is either formatted as: http://thedomain.com/id/xxxx/ or http://thedomain.com/profiles/dzdf/ The URL differs each time but is it possible to make a bit of code that extracts the string between the last two forward slashes? That is, if the variable is the first example, it will out put "xxxx" or if the variable is the second one, it will output "dzdf" but the output is not limited to these two. Sorry if I'm being a bit confusing. Any assistance is much appreciated Link to comment https://forums.phpfreaks.com/topic/241894-get-last-part-of-string/ Share on other sites More sharing options...
AyKay47 Posted July 13, 2011 Share Posted July 13, 2011 You can use preg_match to accomplish this $pattern = '~^http://[\w\d]+\.[com|net|org|gov]/[\w\d]+/([\w\d])+/$~i'; //can add more extensions $subject = "http://thedomain.com/id/xxxx/"; preg_match($pattern,$subject,$matches); print $matches[0]; //first match Link to comment https://forums.phpfreaks.com/topic/241894-get-last-part-of-string/#findComment-1242214 Share on other sites More sharing options...
OhTheNoes Posted July 13, 2011 Author Share Posted July 13, 2011 You can use preg_match to accomplish this $pattern = '~^http://[\w\d]+\.[com|net|org|gov]/[\w\d]+/([\w\d])+/$~i'; //can add more extensions $subject = "http://thedomain.com/id/xxxx/"; preg_match($pattern,$subject,$matches); print $matches[0]; //first match Sorry, I'm not very good at PHP, please bear with me... I tried doing this $pattern = '~^http://[\w\d]+\.[com|net|org|gov]/[\w\d]+/([\w\d])+/$~i'; //can add more extensions $subject = $urlInput preg_match($pattern,$subject,$matches); $urlResult = $matches[0]; //first match (bit of html code here so the php breaks into two) echo $urlResult but it's coming out as blank? Link to comment https://forums.phpfreaks.com/topic/241894-get-last-part-of-string/#findComment-1242220 Share on other sites More sharing options...
premiso Posted July 13, 2011 Share Posted July 13, 2011 If you are simplying parsing a url, use the parse_url function. $path = parse_url($url, PHP_URL_PATH); echo $path; And a correction to AyKay's code, the $matches[0] does not return the first match, it returns the fully matched string. $matches[1] returns the first match as identified by the parenthesis. Link to comment https://forums.phpfreaks.com/topic/241894-get-last-part-of-string/#findComment-1242237 Share on other sites More sharing options...
OhTheNoes Posted July 13, 2011 Author Share Posted July 13, 2011 Hello premiso, The code you gave me results in either /id/xxxx/ or /profiles/zzzz/ but I would like it to only output xxxx or zzzz Would that be possible? Link to comment https://forums.phpfreaks.com/topic/241894-get-last-part-of-string/#findComment-1242238 Share on other sites More sharing options...
premiso Posted July 13, 2011 Share Posted July 13, 2011 Yes, with using explode and end $path = trim($path, '/'); // remove any trailing slashes $path = explode('/', $path); $last = end($path); echo $last; Should do it. Link to comment https://forums.phpfreaks.com/topic/241894-get-last-part-of-string/#findComment-1242240 Share on other sites More sharing options...
OhTheNoes Posted July 13, 2011 Author Share Posted July 13, 2011 Works perfectly. Thank you so much! Link to comment https://forums.phpfreaks.com/topic/241894-get-last-part-of-string/#findComment-1242242 Share on other sites More sharing options...
AbraCadaver Posted July 13, 2011 Share Posted July 13, 2011 echo basename('http://thedomain.com/id/xxxx/'); You can use parse_url() first if you are unsure of the format and there might be a filename present. Link to comment https://forums.phpfreaks.com/topic/241894-get-last-part-of-string/#findComment-1242256 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.