Search code examples
phpexplodesubstr

How to get index 0 of a php substring


Hie I have the below code. It works fine but i have a feeling it smells, is there a better way to write it than this. The objective is to get '58a9a55e1c54f'.

$value = 'songs/initial/58a9a55e1c54f.flac';

$test = substr($value, 16);
$test = explode(".",$test);
$finalResult = $test[0];

Solution

  • The following will work for any arbitrary /-delimited path name followed by a . separated string, as long as the string doesn't contain a /:

    $value = 'songs/initial/58a9a55e1c54f.flac'; $test = explode(".",end(explode("/",$value))); $finalResult = $test[0];