Jump to content

preg_replace ALL alpha numeric things and special characters before..


Gabrax

Recommended Posts

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 :)

 

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?

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 />';
}
?>

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?

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.