User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 391,991 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,307 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 180 | Replies: 5
Reply
Join Date: Oct 2006
Location: UK
Posts: 136
Reputation: MaxMumford is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
MaxMumford's Avatar
MaxMumford MaxMumford is offline Offline
Junior Poster

delete .jpg from end of variable

  #1  
Jul 1st, 2008
Hi all,

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

$name = $_POST["name"];

Thanks

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
Last edited by MaxMumford : Jul 1st, 2008 at 3:12 pm.
Economizerz Hosting
Price Beater Promise

10% Cheaper Hosting, & Double Bandwidth and Web Space
Click here to apply
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2008
Posts: 6
Reputation: allexxei is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
allexxei allexxei is offline Offline
Newbie Poster

Re: delete .jpg from end of variable

  #2  
Jul 1st, 2008
$name = str_replace('.jpg','',$_POST["name"]);

This is the easiest way. But only if you are sure that are all jpg.
Reply With Quote  
Join Date: Oct 2006
Location: UK
Posts: 136
Reputation: MaxMumford is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
MaxMumford's Avatar
MaxMumford MaxMumford is offline Offline
Junior Poster

Re: delete .jpg from end of variable

  #3  
Jul 1st, 2008
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
Last edited by MaxMumford : Jul 1st, 2008 at 3:20 pm.
Economizerz Hosting
Price Beater Promise

10% Cheaper Hosting, & Double Bandwidth and Web Space
Click here to apply
Reply With Quote  
Join Date: Jul 2008
Posts: 6
Reputation: allexxei is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
allexxei allexxei is offline Offline
Newbie Poster

Re: delete .jpg from end of variable

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

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

You should remove the prints and addd a return line

function remove_ext($str) {
$noext = preg_replace('/(.+)\..*$/', '$1', $str);
return $noext;
}
Last edited by allexxei : Jul 1st, 2008 at 3:21 pm.
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: delete .jpg from end of variable

  #5  
Jul 1st, 2008
  1. $filename = "test123.jpg";
  2. $file = substr($filename,0,strpos($filename,"."));
  3. 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 !
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Oct 2006
Location: UK
Posts: 136
Reputation: MaxMumford is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
MaxMumford's Avatar
MaxMumford MaxMumford is offline Offline
Junior Poster

Re: delete .jpg from end of variable

  #6  
Jul 2nd, 2008
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
Economizerz Hosting
Price Beater Promise

10% Cheaper Hosting, & Double Bandwidth and Web Space
Click here to apply
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 9:46 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC