Gabrax Posted December 21, 2009 Share Posted December 21, 2009 I want to replace the name of an uploaded image by taking out all the a-z numbers and weird characters but ignore .jpg or .gif , etc lol where the heck do i start? Quote Link to comment Share on other sites More sharing options...
Roq Posted December 21, 2009 Share Posted December 21, 2009 You're going to use a regular expression something preg_replace <?php $str = 'iamacr4zyfilename.jpg'; $str = preg_replace('/^.*\./', 'file1.', $str); echo $str; ?> That should change all characters before the . to 'file1'. Don't know exactly if this is what you're looking for but you will definitely need to use some sort of regular expression to solve this problem Quote Link to comment Share on other sites More sharing options...
salathe Posted December 22, 2009 Share Posted December 22, 2009 So you want to remove everything except the file extension, or just certain things? Your desired result isn't very clear: could you give some examples of what goes in and you expect to come out? Quote Link to comment Share on other sites More sharing options...
Gabrax Posted December 22, 2009 Author Share Posted December 22, 2009 I want to keep whatever extension is associated with the image but replace the filename entirely without removing .jpg or .gif ,etc Quote Link to comment Share on other sites More sharing options...
Deoctor Posted December 22, 2009 Share Posted December 22, 2009 u can use it like this there is one function in php http://php.net/manual/en/function.getimagesize.php which can tell you what is the type of image then u store the extension value with some string and when ever u rename the file just add the respective extension just try this <?php $info = getimagesize("1.jpg"); foreach($info as $key => $value) { echo $key . ' - ' . $value . '<br />'; } ?> Quote Link to comment Share on other sites More sharing options...
salathe Posted December 22, 2009 Share Posted December 22, 2009 You could also use: <?php $str = 'iamacr4zyfilename.jpg'; $ext = '.' . pathinfo($str, PATHINFO_EXTENSION); echo $ext; ?> Quote Link to comment Share on other sites More sharing options...
Gabrax Posted December 22, 2009 Author Share Posted December 22, 2009 You guys are awesome! Thanks!! I have one last question how do I understand those expressions within the preg_replace function? this is what I am talking about : preg_replace ( '/^.*\./' ) where can I find more info on understanding what that expression means and how to construct my own for other instances where I want to use preg_replace? Quote Link to comment Share on other sites More sharing options...
cags Posted December 22, 2009 Share Posted December 22, 2009 Without buying a book, reading and understanding as much of these two links as possible should be a very good foundation (that's how I learnt). http://www.php.net/PCRE http://www.regular-expressions.info Quote Link to comment Share on other sites More sharing options...
salathe Posted December 22, 2009 Share Posted December 22, 2009 And ask for Mastering Regular Expressions for Christmas. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.