I have a class that uses PHP's ereg() which is deprecated.
Looking on PHP.net I thought I could just get away and change to preg_match()
But I get errors with the regular expressions or they fail!!
Here are two examples:
function input_login() {
if (ereg("[^-_#\.A-Za-z0-9]", $input_value)) { // WORKS
// if (preg_match("[^-_#\.A-Za-z0-9]", $input_value)) { // FAILS
echo "is NOT alphanumeric with characters - _ # . allowed";
}
}
// validate email
function validate_email($email) {
// return eregi("^[_\.0-9a-zA-Z-]+#([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$", $email); // FAILS
}
You forgot the delimiters:
if (preg_match("/[^-_#.A-Za-z0-9]/", $input_value))
Also, the dot doesn't need to be escaped inside a character class.
For your validation function, you need to make the regex case-insensitive by using the i modifier:
return preg_match('/^[_.0-9a-zA-Z-]+#([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i', $email)
I can't suppress the suspicion anymore that people simply don't like my +#example.org email address (I changed the right part only). It is an absolutely normal address, it's valid, short and easy to type. Simply people don't like it! One cannot register on any page using that mail.
So, why don't be nice and use PHPs Filter extension to verify the mail or to use a PCRE, which definitely allows all valid emails in use (excluding only the #[] ones):
/^[^#]+#(?:[^.]+\.)+[A-Za-z]{2,6}$/
Thanks for saving my email, it's a dieing species!
try preg_match("/[^-_#\.A-Za-z0-9]/", $input_value)
Related
I am using the following FUNCTION to extract email address from text.
function is_valid_email($email) {
if (preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.([a-z]){2,4})$/',$emailss)) return true;
else return false;
}
It is working very smoothly, but on problem:
an email with "dash" is not working:
for example:
info-test#web-site.com comes out: test#web
Please advise.
Dash has special meaning in regular expression. So cant be used directly and need to be escaped using backslash. following is updated code:
function is_valid_email($email) {
if (preg_match('/^[_a-z0-9\-]+(\.[_a-z0-9\-]+)*#[a-z0-9\-]+(\.[a-z0-9\-]+)*(\.([a-z]){2,4})$/',$emailss)) return true;
else return false;
}
You should escape the dash character, as it has a special meaning (range) in the used context:
[_a-z0-9\-]
There are myriads of problems with that e-mail validation regexp. For example, it won't pass any of perfectly valid modern national TLDs and it honestly thinks that TLD has maximum 4 letters in it. It doesn't allow arbitrary number of dots . in user account part, it doesn't allow pluses +, etc.
Generally, a good practice of validating e-mails boils down to:
Minimal validation - just check that there's # there and that's all.
Just send that e-mail - don't check anything else. If it will be sent - then it's indeed a valid e-mail.
For more details, take a look at http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/ or any similar articles.
This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 8 years ago.
1st post here so bear with me lol
just wanted to check my work here i have a form that validates a email address but with the change to php 5.3 now errors
can some one please look over my change and tell me what im missing as it not working
OLD WAY
function valid_email($email)
{
// check an email address is valid
if (ereg('^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+#([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$', $email))
return true;
else
return false;
}
NEW WAY
function valid_email($email)
{
// check an email address is valid
if (preg_match(('^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+#([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$', $email))
return true;
else
return false;
}
form sends with no email validation.
i no its prob some thing simple but i cant work it out
thanks in advance
Do not use regular expressions for validating email addresses or hyperlinks. Also, as far as I know, ereg has been deprecated. Use preg from now on with regular expressions. The code for validating email addresses is:
if (filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
echo "This email address: $email_address is invalid. Enter a new one please";
}
If you must use a regular expression, use this RFC-822 regex
, but that may be a bit too complicated for what you're trying to do.
First unlike ereg, with preg_match you need to enclose the pattern within a delimiter, which is usually "/" though you can use other characters too if you want. For example,
preg_match('/^pattern$/', $email)
Secondly in your code there is an extra "(" after the function name "preg_match((" which should be "preg_match(" instead.
You might want to consider using the following regular expressions. It prevents domain name starting or ending with a dash such as username#-domain-.com but still allow dash within domain name such as username#my-domain.com
function isValidEmail($email)
{
return preg_match('/\A[a-z0-9]+([-._][a-z0-9]+)*#([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,4}\z/', $email)
&& preg_match('/^(?=.{1,64}#.{4,64}$)(?=.{6,100}$).*/', $email);
}
See validate email address using regular expression in PHP.
Before anyone flames me, I'd like to say I have tried all solutions here on SO as well as google but nothing works :(. I need some enlightenment :)
Here is my custom form_validation callback function.
Code Snippet
function _check_valid_text($text_string)
{
if(empty($text_string)) return TRUE;
if(!preg_match("/^[\pL\pN_ \w \-.,()\n]+$/i", $text_string))
{
// Set the error message:
$this->form_validation->set_message('_check_valid_text', 'Invalid Character or symbol');
return FALSE;
}
return TRUE;
}
WHAT I WANT:
Allow all alphanumeric characters [a-z, A-Z & 0-9]
Allow accents
Allow the following too: space, open brace, close brace, dash, period and comma
Strangely
für works, i.e. no error (YAY!).
trägen does not :(
Can someone please tell me where I am going wrong?
PS: Please don't provide solutions where your solution is to include the char that is not getting recognized into the preg_match string (e.g. add ä into the string). I am looking for a generic solution that works.
PPS I want to make a suggestion to the community. How about having a library of regex expressions for various cases? One can write a regex and once someone else verifies that it is working it can be accepted. Just a suggestion though. I assume there are more newcomers here like me who get stuck a lot in regex-hell :)
You must add the u modifier to inform the regex engine that your string must be treated as an unicode string. Note too that you can reduce your character class, example:
$subjects = array('Wer nur den lieben Gott läßt walten',
'Sei gegrüßet Jesu gütig',
'Komm, Gott, Schöpfer, Heiliger Geist',
'←↑→↓');
$pattern = '~^[\pL\pN\s,.()_-]++$~u';
foreach ($subjects as $subject) {
if (preg_match($pattern, $subject, $match))
echo '<br/>True';
else
echo '<br/>false';
}
There is no generic solution to handle this because no predefined set of characters that meets your needs exists.
However, you can add the range of accented characters.
[ ,-\.0-9A-Za-z\{\}À-ž]
Also, see How to allow utf-8 charset in preg_match? for information on matching against a specific character set.
I've written this regex to check for valid emails: /^[-a-z0-9._]+#[-a-z0-9._]+\.+[a-z]{2,6}$/i
I want it to work for emails like name1+name2#domaine.com
How can I fix this regex?
I Have a simpler solution.
if(filter_var($email,FILTER_VALID_EMAIL))
{
//true
}
this would be sufficient in most cases, this actually runs an regular check in C which in turn would be faster but if you wish to have control over the reg-ex in your application then the regex below is what's used for this check:
/^((\\\"[^\\\"\\f\\n\\r\\t\\b]+\\\")|([\\w\\!\\#\\$\\%\\&\\'\\*\\+\\-\\~\\/\\^\\`\\|\\{\\}\\=\\?]+(\\.[\\w\\!\\#\\$\\%\\&\\'\\*\\+\\-\\~\\/\\^\\`\\|\\{\\}\\=\\?]+)*))#((\\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\\-])+\\.)+[A-Za-z\\-]+))$/D
Another tip i will give you is that a user may enter an email address such as: invalid#dontexists.com which would then bypass your checks for a valid email, if you wan't to make sure that dontexists.com is running an email server is do:
$has_mx_server = (bool)checkdnsrr($domain,"MX");
if the domain has a registered MX Record the chances of the email being faked is reduced by a good chunk.
First part
[-a-z0-9._]+
does not accept right now plus sign. Expand it:
[-+a-z0-9._]+
Try
/^[-a-z0-9._+]+#[-a-z0-9._]+\.+[a-z]{2,6}$/i
Place the + inside the braces and escape it with a backslash
/^[-a-z0-9._\+]+#[-a-z0-9._]+\.+[a-z]{2,6}$/i
"+" is a meta character meaning to search for 1 or more occurrence, therefore, to search for the actual character, it must be escaped.
I have used the pattern /[a-z0-9_]+/i within the function:
function validate_twitter($username) {
if (eregi('/[a-z0-9_]+/i', $username)) {
return true;
}
}
With this, I test if the input is a valid twitter username, but i'm having difficulties as it is not giving me a valid result.
Can someone help me find a solution.
To validate if a string is a valid Twitter handle:
function validate_username($username)
{
return preg_match('/^[A-Za-z0-9_]{1,15}$/', $username);
}
If you are trying to match #username within a string.
For example: RT #username: lorem ipsum #cjoudrey etc...
Use the following:
$string = 'RT #username: lorem ipsum #cjoudrey etc...';
preg_match_all('/#([A-Za-z0-9_]{1,15})/', $string, $usernames);
print_r($usernames);
You can use the latter with preg_replace_callback to linkify usernames in a string.
Edit: Twitter also open sourced text libraries for Java and Ruby for matching usernames, hash tags, etc.. You could probably look into the code and find the regex patterns they use.
Edit (2): Here is a PHP port of the Twitter Text Library: https://github.com/mzsanford/twitter-text-php#readme
Don't use / with ereg*.
In fact, don't use ereg* at all if you can avoid it. http://php.net/preg_match
edit: Note also that /[a-z0-9_]+/i will match on spaces are invalid and not-a-real-name. You almost certainly want /^[a-z0-9_]+$/i.
S
I believe that you're using the PCRE form, in which case you should be using the preg_match function instead.
eregi() won't expect any / or additional toggles. Just use eregi('[a-z0-9_]+')
Your regular expression is valid, although it allows spaces FYI. (If you want to test out regular expressions I recommend: http://rubular.com/).
The first issue here is your use of eregi which is deprecated as of PHP 5.3. It is recommended that you use preg_match instead, it has the same syntax. Give that a try and see if it helps.
PHP Documentation for preg_match: http://www.php.net/manual/en/function.preg-match.php
PHP Documentation for eregi: http://php.net/manual/en/function.eregi.php
Twitter user names have from 1 to 15 chars... so this could be even better with /^[a-z0-9_]{1,15}$/i.