Grab remaining text after last “/” in a php string

前端 未结 8 1971
我寻月下人不归
我寻月下人不归 2020-12-28 11:47

So, lets say I have a $somestring thats holds the value \"main/physician/physician_view\".

I want to grab just \"physician_view\". I want it to also wo

相关标签:
8条回答
  • 2020-12-28 12:41

    Use basename, which was created for this exact purpose.

    0 讨论(0)
  • 2020-12-28 12:48

    May be Late...

    The Easiest way inbuilt in PHP to grab the last string after last / could be by simply using the pathinfo function.

    In fact, you could check this for yourself,

    $urlString = 'path/to/my/xximage.png';
    
    $info = pathinfo($urlString);
    

    You could do:

    var_dump($info);
    

    this will give you something like:

    'dirname' => string 'path/to/my/' 
    'basename' => string 'xximage.png'
    'extension' => string 'png' (length=3)
    'filename' => string 'xximage' 
    

    so to extract the images out of that link you could do:

     $img=$info['basename'];
     $extension=$info['extension'];
     ///etc...
    
    echo $img.".".$extension; //xximage.png
    

    Something small, but can make you Grow_Gray_Hair_Prematurely

    0 讨论(0)
提交回复
热议问题