PHP get everything in a string before underscore

前端 未结 4 1842
既然无缘
既然无缘 2020-12-21 10:09

I have this code here:

$imagePreFix = substr($fileinfo[\'basename\'], strpos($fileinfo[\'basename\'], \"_\") +1);

this gets me everything after

4条回答
  •  梦毁少年i
    2020-12-21 10:40

    I think the easiest way to do this is to use explode.

    $arr = explode('_', $fileinfo['basename']);
    echo $arr[0];
    

    This will split the string into an array of substrings. The length of the array depends on how many instances of _ there was. For example

    "one_two_three"
    

    Would be broken into an array

    ["one", "two", "three"] 
    

    Here's some documentation

提交回复
热议问题