Convert reg to preg_match

Andy.N

Well-known member
Anyone with experience in regex can help me convert these since it won't work in PHP 7 anymore.
Code:
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {

I'd like to replace ereg with preg_match
Thank you
 
For the most part, you likely just need to prefix and suffix them with a delimiter. The delimiter should either be a character that doesn't appear in the regex itself, or the regex sufficiently escaped. Generally / and # are the best delimiters to use. If you need to escape any of the characters then you would do that with a backslash.

For example:
Code:
if (!preg_match("/^[^@]{1,64}@[^@]{1,255}$/", $email)) {
 
preg_quote($email, '#') (if # is your delimiter) may also be a good idea just in case someone decides to try to break your app by entering an email with the delimiter character in it.


Fillip
 
So basically I will just put / after and before the quotation "
if (!ereg("/^[^@]{1,64}@[^@]{1,255}$/", $email)) {
if (!ereg("/^\[?[0-9\.]+\]?$/", $email_array[1])) {

The last one is a bit tricky, I"m not sure where to start since it has many characters.
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
 
For information, mb_ereg is a direct replacement of ereg which is not deprecated in PHP 7. Thanks for all that helps.
 
Code:
if (!preg_match("/^(([A-Za-z0-9!#$%&'*+\/=?^_`{|}~-][A-Za-z0-9!#$%&'*+\/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/", $local_array[$i])) {
 
Top Bottom