-4

I want to get first file extension in string using php.

now i use this code.

<?php
$string = "aaa.jpgbbb.pngccc.jpg";
if( strpos( $string, $needle ) !== false ) {
    list($string) = explode('.png',$string);
    echo $string.".jpg";
}
?>

My code only check file extension in string. but can not get first file extension. How can i do ?

. .

For this string first file extension is jpg.

$string = "aaa.jpgbbb.pngccc.jpg";

. .

For this string first file extension is gif.

$string = "aaa.gifbbb.pngccc.jpg";

. .

For this string first file extension is png.

$string = "aaa.pngbbb.pngccc.jpg";

1 Answers1

0
$string = "aaa.gifbbb.pngccc.jpg";
$pattern = '|\.([^\.]+)$|';
preg_match ( $pattern , $string, $matches);
var_dump($matches);
die;

Try this one

Dmitri Gudkov
  • 2,093
  • 16
  • 15