PHP - trim everything before the period

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

PHP - trim everything before the period

Post by cjkeane »

Hi everyone, I'm wondering if there is a way to trim everything before the period in a variable. For e.g.:

variable = test.jpg
new variable = .jpg

Thanks.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: PHP - trim everything before the period

Post by Eric! »

will you ever have more than one period like test.my.site.gif? Do you just want the file extension?

Code: Select all

$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');

echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
[text]/www/htdocs/inc
lib.inc.php
php
lib.inc[/text]
if you want the dot, $extension=".".$path_parts['extension'];
Post Reply