Hi all,

I just need to delete .jpg form the end of a variable stored like this:

$name = $_POST["name"];

Thanks :D

Max

ps
echo stristr($name, '.', true); // As of PHP 5.3.0, outputs *content of string BEFORE the first full stop*
dosnt work. im guessing its because my php on my server is a different version than 5.3.0
this is the code i tested just to make sure it wasnt a silly mistake

$email = 'USER@EXAMPLE.com';
echo stristr($email, 'e'); // outputs ER@EXAMPLE.com
echo stristr($email, 'e', true); // As of PHP 5.3.0, outputs US

Recommended Answers

All 5 Replies

$name = str_replace('.jpg','',$_POST["name"]);

This is the easiest way. But only if you are sure that are all jpg.

function remove_ext($str) {
$noext = preg_replace('/(.+)\..*$/', '$1', $str);
print "input: $str\n";
print "output: $noext\n\n";
}

I found it on php.net :D Must work very good and shoul remove any extension

You should remove the prints and addd a return line :D

function remove_ext($str) {
$noext = preg_replace('/(.+)\..*$/', '$1', $str);
return $noext;
}

Ah okay that works fine. I actually found this solution just as you sent your reply which is:

$name = substr($_POST['name'], 0, -4); //gets rid of last 4 characters. remove the ", 0" to get rid of first 4 characters. Refer to http://uk2.php.net/manual/en/function.substr.php

That works too but only gets rid of the last 4 letters. In my case im sure they are all jpg but other people may want to use another method.

Thanks for your help.

And thanks allexxei too

$filename = "test123.jpg";
$file = substr($filename,0,strpos($filename,"."));
echo $file;

You can do it this way too. But the problem is, if there is a . in the filename itself, for example, test.123.jpg , then this will return only test and not test.123 . The function given by allexxei is, I think, the best solution !

kk. Its me who is uploading the pictures anyway so it should be fine. But in future ill probably need to use it again :)

Thanks everybody

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.