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
https://forums.phpfreaks.com/topic/44052-solved-ereg_replace-vs-preg_replace/
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: \/

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.

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

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

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.

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.