Jump to content

[SOLVED] ereg_replace vs preg_replace?


notepad

Recommended Posts

Hi,

 

I am new to Regex, but am finding it very interesting so far. I used a little bit on my login script for my website, I set it up to strip out any non-alphanumeric characters (including spaces) using ereg_replace. But I have been told by a couple of people that ereg_replace is out of date and and I should be using preg_replace. Is this true? Here is my current code:

 

$user_check = $_POST['name'];
$strip_username = ereg_replace("[^A-Za-z0-9]", "", $user_check );

$pass_check = $_POST['pass'];
$strip_password = ereg_replace("[^A-Za-z0-9]", "", $pass_check );

 

I am not sure what the proper syntax would be to convert this to preg_replace, so I was hoping I could have a little assistance?

 

Thanks,

 

Brandon

Link to comment
Share on other sites

I dont think ereg is outdated. Its just ereg and preg use two different regular expressions. ereg uses POSIX-extended and preg uses PCRE (Perl-compatible regular expressions).

 

PCRE has much more power behind it than POSIX. PCRE allows for non-greedy matching, assertions, conditional subpatterns, and a number of other features not supported by the POSIX-extended regular expression syntax.

 

if you want your code to work with PCRE then do this:

$user_check = $_POST['name'];
$strip_username = preg_replace("/[^A-Za-z0-9]/", "", $user_check );

 

The only changes needed was to change ereg_replace to preg_replace and added in delimiters at the start and end of the regex pattern. A delimiter can be a non-alphanumeric character to mark the start and end of the pattern, I normally use slashes - /. Note if you use the same character in your pattern as your delimiter you must escape them by placing a backslash in front of it, example: \/

Link to comment
Share on other sites

Hey Wildteen,

 

Thanks for your reply. I have a much better understanding of the difference now. There is only one thing I am still confused about...

 

The only changes needed was to change ereg_replace to preg_replace and added in delimiters at the start and end of the regex pattern. A delimiter can be a non-alphanumeric character to mark the start and end of the pattern, I normally use slashes - /. Note if you use the same character in your pattern as your delimiter you must escape them by placing a backslash in front of it, example: \/

 

Could you explain that a bit more? Because I didn't see in your code where you used a / and \. I did notice that you used 2 / though.

Link to comment
Share on other sites

Hey Effigy,

 

Thank you for clearing that up, I got it now. But could I bug you for one more thing? I am thinking of making the login use emails instead of usernames. If I were to do that, I would have to allow '@' and '.' in the login script. To add individual characters, do I just add them like this?:

 

$user_check = $_POST['name'];
$strip_username = preg_replace("/[^A-Za-z0-9-@-.]/", "", $user_check );

 

Thanks,

 

Brandon

Link to comment
Share on other sites

Great link, thanks effigy.  :)  Just to see if I understood the link here is the new code I came up with for allowing '_', '-', '.' and '@':

 

$user_check = $_POST['name'];
$strip_username = preg_replace("/[^_-A-Za-z0-9@.]/", "", $user_check );

 

As for 'requiring' it... I don't know how to do that, this is the first regex code I have done...

Link to comment
Share on other sites

The hyphen should be at the front, which technically follows the ^ since it is modifying the entire character class.

 

You're removing invalid e-mail characters, but you're not checking the format of the e-mail. For example, your pattern will allow "me@", "abc", or ".x." as a valid e-mails.

 

See the e-mail examples here.

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.