Jump to content

[SOLVED] Using ereg_replace()


sh0wtym3

Recommended Posts

Hey All,

 

This a snippet from my upload script. At the moment it replaces spaces with "_" and makes the title lowercase.

 

$file = str_replace(' ', '_', $_POST['title']);
   $file = strtolower($file);

 

I also have a Javascript code that restricts the user from typing certain characters, such as apostrophes.

 

Apparently Javascript doesn't work on all browsers as a user just uploaded a file, with a title that has an apostrophe in it, which created all sorts of problems for me. (I cannot delete or rename the file in my ftp)

 

I added 3 more lines that will hopefully prevent this from happening in the future:

 

   $file = str_replace(' ', '_', $_POST['title']);
   $file = strtolower($file);
   $pattern="*^$%&()#@!'";
   $replace="";
   ereg_replace($file,$pattern,$replace);

 

But it doesn't seem to work?

 

If you need to see more of my code just let me know. Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/132354-solved-using-ereg_replace/
Share on other sites

Sorry looks like somebody else asked a similar question in another thread, and the solution (which worked for me as well) is:

 

I would recommend not using ereg, as of PHP6, it will not be supported in the core by default (I think it will only be an extension). You should use preg instead. Here is a good starter thread for that. You can also read about PCRE regex here.

 

$str = 'testing 123...';
$str = preg_replace('#[^a-z0-9]#', '', $str);
echo $str;

 

Ouput:

testing123

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.