Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all data after last dot using php?

Tags:

php

substr

strpos

How to remove all data after last dot using php ?

I test my code. It's echo just aaa

I want to show aaa.bbb.ccc

How can i do that ?

<?PHP
$test = "aaa.bbb.ccc.gif";
$test = substr($test, 0, strpos($test, "."));
echo $test;
?>
like image 872
mongmong seesee Avatar asked Feb 08 '16 09:02

mongmong seesee


2 Answers

You can utilize the function of pathinfo() to get everything before the dot

$str = "aaa.bbb.ccc.gif";
echo pathinfo($str, PATHINFO_FILENAME); // aaa.bbb.ccc
like image 121
Andrew Avatar answered Sep 19 '22 02:09

Andrew


You can try this also -

$test = "aaa.bbb.ccc.gif";

$temp = explode('.', $test);

unset($temp[count($temp) - 1]);

echo implode('.', $temp);

O/P

aaa.bbb.ccc

strpos — Find the position of the first occurrence of a substring in a string

You need to use strrpos

strrpos — Find the position of the last occurrence of a substring in a string

$test = "aaa.bbb.ccc.gif";
$test = substr($test, 0, strrpos($test, "."));
echo $test;

O/P

aaa.bbb.ccc
like image 28
Sougata Bose Avatar answered Sep 20 '22 02:09

Sougata Bose