wordpress extract media url

 $path = parse_url($post->guid, PHP_URL_PATH);
      echo "<pre>";
      print_r($path);
       echo "<br>";

here i get

/wp-content/uploads/2014/01/kl-2-256.png

Read More

/wp-content/uploads/2014/04/bg-eBook.pdf

here i want to remove /wp-conent/uploads from these paths and extract only year month and image name

i tried with

  $segments = explode('/', rtrim($path, '/'));

but not working properly every time

is there any proper and best solution?

Related posts

Leave a Reply

2 comments

  1. Would something like this work for you?

    $path = parse_url($post->guid, PHP_URL_PATH);
    $path = str_replace("wp-content/uploads/", "", $path);
    
    echo $path;
    
  2. Use the list() construct to map the three data you need. The code is exploding the path by / and then looks from behind and passes those values to your mapped variables of list.

    $path = '/wp-content/uploads/2014/01/kl-2-256.png';
    list($year,$month,$image)=array_slice(explode('/',$path),-3,3);
    

    You can then print $year,$month and $image separately.