i would like to know, if we can trim below string start from back until dash ?
String
doc/20-05-2013_08-44-19_Rusly_60_sample1.pdf
doc/20-05-2013_08-44-19_Rusly_60_logo.png
Output
sample1.pdf
logo.png
i would like to know, if we can trim below string start from back until dash ?
String
doc/20-05-2013_08-44-19_Rusly_60_sample1.pdf
doc/20-05-2013_08-44-19_Rusly_60_logo.png
Output
sample1.pdf
logo.png
Use this
$str = "doc/20-05-2013_08-44-19_Rusly_60_sample1.pdf";
echo substr($str,strrpos($str,'_'));
Simply explode string and get last element from array like
$arr = explode("_", 'doc/20-05-2013_08-44-19_Rusly_60_sample1.pdf');
echo end($arr);
Try strchr
$str = "doc/20-05-2013_08-44-19_Rusly_60_sample1.pdf";
echo substr(strrchr($str, "_"), 1);
Use the following functions: http://php.net/manual/en/function.strrpos.php http://php.net/manual/en/function.substr.php
strrpos looks when the last occurence of a charachter is (example it is _) When no occurence is found it returns false. Then with substr with get the string
Then the code will be
$start = (strrpos('_', $string) !== false) ? strrpos($string) : 0;
$new_string = substr($string, $start);
you could do:
$value = array_pop( explode( "_", $yourVar) );
echo $value;
Should we calculate a position of the latest dash?
What if doc/20-05-2013_08-44-19_Rusly_60_
prefix has constant 33 length?
May result be samp_le1.pdf
?
echo substr( $str, 33 );