Jump to content

[SOLVED] How can I get the value between two slashes?


Recommended Posts

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;
?>

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.

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;
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.