-1

I have string "katrina.bhuvnesh.jpg" , I want to get jpg that means substring that matches the last dot(.)

I looked in substr() function but it's not working exactly what I need.

Please give me any function of PHP that matches string from last and returns substring from that point.

Thanks!!

user2307273
  • 29
  • 2
  • 9

2 Answers2

5

Use pathinfo() like this:

 $extension = pathinfo("katrina.bhuvnesh.jpg", PATHINFO_EXTENSION);
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
1

You can use 2 function calls:

$filename = 'somefile.sometext.jpg';

$type = end(explode('.', $filename));

// $type is now 'jpg'

Alternatively, you can use substr:

$type = substr($filename, strrpos($filename, '.'));
Phil Cross
  • 9,017
  • 12
  • 50
  • 84