How to delete all data after the last point using php?

How to delete all data after the last point using php?

I am checking my code. This is an echo of everythingaaa

I want to show aaa.bbb.ccc

How can i do this?

<?PHP
$test = "aaa.bbb.ccc.gif";
$test = substr($test, 0, strpos($test, "."));
echo $test;
?>
+4
source share
2 answers

You can also try this -

$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
+4
source

You can use the function pathinfo()to get everything to the point.

$str = "aaa.bbb.ccc.gif";
echo pathinfo($str, PATHINFO_FILENAME); // aaa.bbb.ccc
+9
source

Source: https://habr.com/ru/post/1627756/


All Articles