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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.