1

Getting the value from url which is between the forward slashes. Like

http://localhost/test/manage-users/1230/

output: 1230

I also found some help full answers but they are not satisfy my case properly. Their url is hard coded i.e, their url cant change but in my case url change if user redirect from different pages.

Get the value of a URL after the last slash

Different url when user redirect from different pages.

  1. http://localhost/test/manage-users/1230/?value=1230
  2. http://localhost/test/manage-users/1230/
  3. http://localhost/test/manage-users/1230/test1/?value=1230

So i want a regex which full fill my all cases.

Community
  • 1
  • 1
ksr89
  • 249
  • 3
  • 9

3 Answers3

1

RegEx you need is

\d+(?=\/)

$re = '/\d+(?=\/)/'; 
$str = 'http://localhost/test/manage-users/1230/?value=1230'; 

preg_match($re, $str, $matches);

It is working for all three cases you have mentioned.

Check Demo

Harpreet Singh
  • 2,651
  • 21
  • 31
0

You could try the below regex to get the number between slashes,

\/(\d+)\/

DEMO

Your php code would be,

<?php
$re = '~\/(\d+)\/~'; 
$str = 'http://localhost/test/manage-users/1230/?value=1230'; 
preg_match($re, $str, $matches);
echo $matches[1];
?> // 1230
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

I don't know if it is possible or not but i have a suggestion. Use URL rewriting.

Feel free to shape your URL's. For example

http://localhost/test/manage-users.php?value=1230

And a little help from this link: http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

You can make it look like:

http://localhost/test/manage-users/1230/
Ozan Atmar
  • 270
  • 1
  • 3
  • 11