Swift-R Posted August 24, 2009 Share Posted August 24, 2009 (edited) I've been using this code to validate user names. It only allows 0-9, a-z, A-Z, spaces... if ( ! eregi ( "^[0-9A-Z_[:space:]]{1,}$", $username ) ) But now, in PHP 5.3 the ereg and eregi functions are deprecated and will be deleted in PHP 6.0. How can I do this without eregi? Edited August 24, 2009 by MNunes2 Link to comment https://www.neowin.net/forum/topic/815802-php-eregi-replacement/ Share on other sites More sharing options...
0 mikeaag Posted August 24, 2009 Share Posted August 24, 2009 I might be compleletly wrong here, but i think this might be what your looking for. If its not, i'll go put the noob hat back on and sit in the corner :p Link to comment https://www.neowin.net/forum/topic/815802-php-eregi-replacement/#findComment-591485772 Share on other sites More sharing options...
0 Lant Posted August 24, 2009 Share Posted August 24, 2009 The documentation at http://uk2.php.net/function.ereg says that http://uk2.php.net/manual/en/function.preg-match.php is the alternative to ereg. I'm not exactly a master of regex but would the following be an alternative? if ( !preg_match("^[0-9A-Za-z_ ]+$", $username) ) Link to comment https://www.neowin.net/forum/topic/815802-php-eregi-replacement/#findComment-591485940 Share on other sites More sharing options...
0 nvme Posted August 24, 2009 Share Posted August 24, 2009 you're close. preg_* functions use perl regular expressions so they have a little extra syntax to them. preg_match("/^[0-9a-z_ ]+$/i", &username) mainly the starting and ending slash. the letter 'i' after the ending slash says "do a case insensitive search". that way you can remove the extra A-Z character class. Link to comment https://www.neowin.net/forum/topic/815802-php-eregi-replacement/#findComment-591487410 Share on other sites More sharing options...
0 Swift-R Posted August 24, 2009 Author Share Posted August 24, 2009 nvme said: you're close. preg_* functions use perl regular expressions so they have a little extra syntax to them. preg_match("/^[0-9a-z_ ]+$/i", &username) mainly the starting and ending slash. the letter 'i' after the ending slash says "do a case insensitive search". that way you can remove the extra A-Z character class. And what does this +$ and this ^ mean? And where can I learn more about this? Link to comment https://www.neowin.net/forum/topic/815802-php-eregi-replacement/#findComment-591487812 Share on other sites More sharing options...
0 nvme Posted August 25, 2009 Share Posted August 25, 2009 a carrot (^) means to assert position at the beginning of the string. so basically.. this pattern needs to start at the very beginning of the string. the dollar sign ($) means assert position at the end of the string. so the strings needs to end with this pattern as well. the stuff between the square braces are known as a character class. a group of characters that must exist (in this case). if you left the plus off after the character class then that character class would only be required to match one character. the plus means match at least one of the characters in the character class.. but the more it finds in next to each other the more it will match. so when you put it all together it says that some string must start with and end with some number, letter, space, or any of the symbols (&, #, _) and it must exist at least once but can exist as many times as possible. if you leave the carrot or dollar sign off of the expression then a pattern could be satisfied anywhere in the string and would be considered a match. for example.. if you used the pattern "[0-9a-z_ ]+" and tested it against the string "abc;.123" it would match two patterns: "abc" and "123". using the dollar sign and carrot it would start matching and get to "abc" then it would see a semicolon and because a semicolon isnt in the character class the pattern fails because the entire string wasnt the match for the pattern. the key thing to remember is that a regular expression will look for any match that it can find if you dont use constraints like these. Link to comment https://www.neowin.net/forum/topic/815802-php-eregi-replacement/#findComment-591488084 Share on other sites More sharing options...
0 Swift-R Posted August 25, 2009 Author Share Posted August 25, 2009 It seems I will have to learn a lot about this function since ereg functions wont be available anymore. :-( Thanks for the information. Link to comment https://www.neowin.net/forum/topic/815802-php-eregi-replacement/#findComment-591488254 Share on other sites More sharing options...
0 Popcorned1 Posted August 25, 2009 Share Posted August 25, 2009 nvme said: a carrot (^) means to assert position at the beginning of the string. so basically.. this pattern needs to start at the very beginning of the string.the dollar sign ($) means assert position at the end of the string. so the strings needs to end with this pattern as well. the stuff between the square braces are known as a character class. a group of characters that must exist (in this case). if you left the plus off after the character class then that character class would only be required to match one character. the plus means match at least one of the characters in the character class.. but the more it finds in next to each other the more it will match. so when you put it all together it says that some string must start with and end with some number, letter, space, or any of the symbols (&, #, _) and it must exist at least once but can exist as many times as possible. if you leave the carrot or dollar sign off of the expression then a pattern could be satisfied anywhere in the string and would be considered a match. for example.. if you used the pattern "[0-9a-z_ ]+" and tested it against the string "abc;.123" it would match two patterns: "abc" and "123". using the dollar sign and carrot it would start matching and get to "abc" then it would see a semicolon and because a semicolon isnt in the character class the pattern fails because the entire string wasnt the match for the pattern. the key thing to remember is that a regular expression will look for any match that it can find if you dont use constraints like these. It's caret. lol Link to comment https://www.neowin.net/forum/topic/815802-php-eregi-replacement/#findComment-591488338 Share on other sites More sharing options...
0 nvme Posted August 25, 2009 Share Posted August 25, 2009 Popcorned1 said: It's caret. lol :rofl: lol my fingers must be hungry MNunes2 said: It seems I will have to learn a lot about this function since ereg functions wont be available anymore. :-( Thanks for the information. i learned mostly from using regex buddy and reading their docs on their site. i think it's a great app for easing into regular expressions and testing/debugged really complex ones even after you get the hang of it. http://www.regexbuddy.com/regex.html Link to comment https://www.neowin.net/forum/topic/815802-php-eregi-replacement/#findComment-591488410 Share on other sites More sharing options...
0 Lexx87 Posted February 2, 2011 Share Posted February 2, 2011 Hi guys, been a few years since i've used PHP and was testing out some old stuff I had, and came across the depreciated ereg function. I was using: if (ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $address)) which I picked up form somewhere, have no idea how it works, and so have no idea how to turn it into preg_match. Anyone lend a hand? I tried some solutions already in the thread but didn't have much luck. My function looks like: function valid_email($address) { // check an email address is possibly valid if (ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $address)) return true; else return false; } Link to comment https://www.neowin.net/forum/topic/815802-php-eregi-replacement/#findComment-593658908 Share on other sites More sharing options...
0 Xerax Posted February 2, 2011 Share Posted February 2, 2011 On 24/08/2009 at 14:28, Swift-R said: I've been using this code to validate user names. It only allows 0-9, a-z, A-Z, spaces... if ( ! eregi ( "^[0-9A-Z_[:space:]]{1,}$", $username ) ) But now, in PHP 5.3 the ereg and eregi functions are deprecated and will be deleted in PHP 6.0. How can I do this without eregi? I've been using the following code to validate Xbox Live Gamertags in a Stats application. (which is A-Z, a-z, 0-9, spaces) preg_replace('/[^a-zA-Z0-9\s]/', '', $username); or if you have more descriptive stuff (like can't start/end with space, can't have 2 spaces in a row, can't start in a number, 15 chars long) preg_replace('/^(?=.{1,15}$)[a-zA-Z][a-zA-Z0-9]*(?: [a-zA-Z0-9]+)*$/', '', $username); Link to comment https://www.neowin.net/forum/topic/815802-php-eregi-replacement/#findComment-593658930 Share on other sites More sharing options...
0 Mike Posted February 2, 2011 Share Posted February 2, 2011 On 02/02/2011 at 19:02, Lexx87 said: Hi guys, been a few years since i've used PHP and was testing out some old stuff I had, and came across the depreciated ereg function. I was using: if (ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $address)) which I picked up form somewhere, have no idea how it works, and so have no idea how to turn it into preg_match. Anyone lend a hand? I tried some solutions already in the thread but didn't have much luck. My function looks like: function valid_email($address) { // check an email address is possibly valid if (ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $address)) return true; else return false; } Seeing as you're using newer PHP, I'd suggest filter_var Link to comment https://www.neowin.net/forum/topic/815802-php-eregi-replacement/#findComment-593658954 Share on other sites More sharing options...
0 Hardcore Til I Die Posted February 3, 2011 Share Posted February 3, 2011 If you want to learn about regular expressions then I would suggest reading up on the Perl Regex man page. A more user friendly website for learning about regex is http://www.regular-expressions.info/, but this teaches PCRE (Perl Compatible Regular Expressions). As the name suggests the two are mostly the same, but there are some minor inconsistencies. Link to comment https://www.neowin.net/forum/topic/815802-php-eregi-replacement/#findComment-593661456 Share on other sites More sharing options...
0 Hardcore Til I Die Posted February 3, 2011 Share Posted February 3, 2011 On 02/02/2011 at 19:08, Xerax said: I've been using the following code to validate Xbox Live Gamertags in a Stats application. (which is A-Z, a-z, 0-9, spaces) preg_replace('/[^a-zA-Z0-9\s]/', '', $username); \s matches all whitespace, not just spaces. It will also match tabs, line feeds, carriage returns, which are clearly not allowed in XBL gamertags :p If you want to match only spaces then you should use [^a-zA-Z0-9 ]. Also, rather than match a-zA-Z you could use the /i case-insensitive modifier. With the above changes, the final regex would become: preg_replace('/[^a-z0-9 ]/i','',$username); Or if you prefer, you can use \d instead of 0-9: preg_replace('/[^a-z\d ]/i','',$username); Link to comment https://www.neowin.net/forum/topic/815802-php-eregi-replacement/#findComment-593661508 Share on other sites More sharing options...
0 Hardcore Til I Die Posted February 3, 2011 Share Posted February 3, 2011 On 02/02/2011 at 19:02, Lexx87 said: Hi guys, been a few years since i've used PHP and was testing out some old stuff I had, and came across the depreciated ereg function. I was using: if (ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $address)) which I picked up form somewhere, have no idea how it works, and so have no idea how to turn it into preg_match. Anyone lend a hand? I tried some solutions already in the thread but didn't have much luck. My function looks like: function valid_email($address) { // check an email address is possibly valid if (ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $address)) return true; else return false; } function valid_email($address) { // check an email address is possibly valid return (preg_match('/^[a-z\d_\.\-]+@([a-z\d\-]+)(?:\.(?1))+$/i',$address)) ? true : false; } Your original regex matched things like "my@email....com" which is clearly not a valid email address. This one will not match that. I'm not a PHP programmer so the above may not work if I've got any of the PHP syntax wrong, but I've tried out the regex in another language and it seems to work :) Edit: In fact, if top level domains only ever go two levels deep, like ".co.uk," then this regex is probably more appropriate: function valid_email($address) { // check an email address is possibly valid return (preg_match('/^[a-z\d_\.\-]+@([a-z\d\-]+)(?:\.(?1)){1,2}$/i',$address)) ? true : false; } Link to comment https://www.neowin.net/forum/topic/815802-php-eregi-replacement/#findComment-593661570 Share on other sites More sharing options...
0 AnthonySterling Posted February 3, 2011 Share Posted February 3, 2011 Forget the RegEx. Use a maintained, and native, solution. :) function isEmail($address){ return false !== filter_var($address, FILTER_VALIDATE_EMAIL); } Link to comment https://www.neowin.net/forum/topic/815802-php-eregi-replacement/#findComment-593661584 Share on other sites More sharing options...
Question
Swift-R
I've been using this code to validate user names. It only allows 0-9, a-z, A-Z, spaces...
But now, in PHP 5.3 the ereg and eregi functions are deprecated and will be deleted in PHP 6.0.
How can I do this without eregi?
Edited by MNunes2Link to comment
https://www.neowin.net/forum/topic/815802-php-eregi-replacement/Share on other sites
15 answers to this question
Recommended Posts