papaface Posted December 31, 2007 Share Posted December 31, 2007 Hello, I am trying to get the number inside of this string: user/worstmovieever/video/1957687/someotherparameter/ I need a way of fetching: 1957687 out of there Anyone have any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/83839-solved-how-can-i-get-the-value-between-two-slashes/ Share on other sites More sharing options...
GingerRobot Posted December 31, 2007 Share Posted December 31, 2007 You could do it with a regular expression: <?php $str = 'user/worstmovieever/video/1957687/someotherparameter/'; preg_match('|[0-9]+|',$str,$matches); $num = $matches[0]; echo $num; ?> Or if the number is always in the same place in the string, you could explode by the slashes: <?php $str = 'user/worstmovieever/video/1957687/someotherparameter/'; $bits = explode('/',$str); $num = $bits[3]; echo $num; ?> Quote Link to comment https://forums.phpfreaks.com/topic/83839-solved-how-can-i-get-the-value-between-two-slashes/#findComment-426686 Share on other sites More sharing options...
papaface Posted December 31, 2007 Author Share Posted December 31, 2007 You could do it with a regular expression: <?php $str = 'user/worstmovieever/video/1957687/someotherparameter/'; preg_match('|[0-9]+|',$str,$matches); $num = $matches[0]; echo $num; ?> Or if the number is always in the same place in the string, you could explode by the slashes: <?php $str = 'user/worstmovieever/video/1957687/someotherparameter/'; $bits = explode('/',$str); $num = $bits[3]; echo $num; ?> Thanks for that. However I think that only works if those are the only numbers in the string. Its likely since the string is dynamic that there will be more numbers in the string. But these will be the only numbers within two slashes. Also the URL is not always the same length, so I need a way of targetting the numbers between the two slashes. Quote Link to comment https://forums.phpfreaks.com/topic/83839-solved-how-can-i-get-the-value-between-two-slashes/#findComment-426688 Share on other sites More sharing options...
GingerRobot Posted December 31, 2007 Share Posted December 31, 2007 If the number is definitely the only one contained between two slashes, try: <?php $str = 'user/worstmovieever/video/1957687/someotherparameter/'; preg_match('|/([0-9]+)/|',$str,$matches); $num = $matches[1]; echo $num; ?> Quote Link to comment https://forums.phpfreaks.com/topic/83839-solved-how-can-i-get-the-value-between-two-slashes/#findComment-426690 Share on other sites More sharing options...
papaface Posted December 31, 2007 Author Share Posted December 31, 2007 Works, thanks Quote Link to comment https://forums.phpfreaks.com/topic/83839-solved-how-can-i-get-the-value-between-two-slashes/#findComment-426691 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.