0

I have this url :

http://localhost/cms/uploads/files/1/images/hd-wallpaper-40.jpg

and need to convert to :(remove all url before 1)

1/images/hd-wallpaper-40.jpg

EDIT:

http://localhost/cms/uploads/files/ is dynamic So Maybe : http://localhost/uploads/files/

how do can I convert this url using php?

Dr.Tricker
  • 545
  • 3
  • 19
NewCod3r
  • 912
  • 1
  • 8
  • 24

4 Answers4

5

I don't know the rules/conditions you need but this is a way:

$url = parse_url('http://localhost/cms/uploads/files/1/images/hd-wallpaper-40.jpg');
echo str_replace('/cms/uploads/files/', '', $url['path']);

UPDATE:

If imagesis static:

$url = parse_url('http://localhost/cms/uploads/files/1/images/hd-wallpaper-40.jpg');
preg_match('/[0-99999]\/.*/', $url['path'], $matches);
echo $matches[0];
michelem
  • 13,169
  • 4
  • 44
  • 60
2

You can use parse_url function to get whatevers after the host (in this case localhost) and then explode the path, slice an array and implode it back:

$str = "http://localhost/cms/uploads/files/1/images/hd-wallpaper-40.jpg";
$parsed_url = parse_url($str);
echo implode("/", array_slice(explode("/", $parsed_url['path']),4));
DannyPhantom
  • 1,012
  • 9
  • 21
  • In that case do `implode("/", array_slice(explode("/", $parsed_url['path']),-3));` which will only take the last 3 items in path (1/images/hd-wallpaper-40.jpg) – DannyPhantom Aug 30 '15 at 07:04
  • only `1` is static. `1` is author id and other is dynamic . `-3` not work n my case – NewCod3r Aug 30 '15 at 07:22
  • `$matches = array(); preg_match('/\/[0-9]+\/.*$/', $parsed_url['path'], $matches);` after that `$matches[0]` will have what you need – DannyPhantom Aug 30 '15 at 07:27
  • You can also do `preg_match('/.*(\/[0-9]+\/.*)$/', $parsed_url['path'], $matches);` if there is a chance of having /1/3/4/5 and you need only /5/... Your result will be in `$matches[1]` – DannyPhantom Aug 30 '15 at 07:31
0

a simple solution may be;

$basepath = 'http://localhost/cms/uploads/files/';
$url = 'http://localhost/cms/uploads/files/1/images/hd-wallpaper-40.jpg';
if (strncmp($basepath, $url, strlen($basepath)) == 0 ) {
    $result = substr($url, strlen($basepath));
} else {
    $result = false;
}
echo $result;
bkilinc
  • 921
  • 1
  • 12
  • 27
  • that is why, it is assigned to a variable. Use anything you want as value; like $basepath = $_SERVER['HTTP_HOST]. '/cms/uploads/files/'; – bkilinc Aug 30 '15 at 10:03
0

Try this, dynamic content with '/1/' will work perfectly

$str = "http://localhost/cms/uploads/files/1/images/1/hd-wallpaper-40.jpg";
//url as string
$pos = strpos($str,'1');    //position of first '1' in url
$len = strlen($str);    //length of url string
$str1 = substr($str,$pos,($len-$pos));  //substring from '1' onwards

There are few drawbacks too. If the '1' occurres before , i mean url = "http://localhost/cms1/uploads/files/1/images, it will give wrong result. If you use dynamic author id, instead of 1, save author id as a variable and insert in strpos(). The below one will be more accurate and you can use dynamic authorID too.

$str = "http://localhost/cms/uploads/files/1/images/1/hd-wallpaper-40.jpg";
//url as string
$authid = 1;    //author id
$searchstring = '/'.$authid.'/';    // create a search string  '/1/'
$pos = strpos($str,$searchstring);  //position of first '/1/' in url
$len = strlen($str);    //length of url string
$str1 = substr($str,$pos+1,($len-$pos));    //substring from '1' onwards
Subin Thomas
  • 1,350
  • 7
  • 18