2

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
rusly
  • 1,504
  • 6
  • 28
  • 62

9 Answers9

4

Use this

$str = "doc/20-05-2013_08-44-19_Rusly_60_sample1.pdf";
echo substr($str,strrpos($str,'_'));
Sudz
  • 4,268
  • 2
  • 17
  • 26
3

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);
Mahmood Rehman
  • 4,303
  • 7
  • 39
  • 76
2

You could solve it like this:

echo array_pop(explode('_', $string));
silkfire
  • 24,585
  • 15
  • 82
  • 105
1

try this

$result = preg_replace('/(?m)^.+_(.+)$/', '$1', $subject);
Cylian
  • 10,970
  • 4
  • 42
  • 55
1

Try strchr

$str = "doc/20-05-2013_08-44-19_Rusly_60_sample1.pdf";

echo substr(strrchr($str, "_"), 1);
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
0
  1. locate the position of the dash with strrpos
  2. use substr($roginal, $dashposition + 1)
Peter Kiss
  • 9,309
  • 2
  • 23
  • 38
0

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);
MKroeders
  • 7,562
  • 4
  • 24
  • 39
0

you could do:

$value = array_pop( explode( "_", $yourVar) );
echo $value;
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
0

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 );
Andrey Volk
  • 3,513
  • 2
  • 17
  • 29