regular expression php pregmatch [duplicate] - php

This question already has answers here:
How to validate an email address in PHP
(15 answers)
Closed 7 years ago.
I have a php regular expression for email formatting. I used this code below
if ( !(preg_match('/^\w+#[\w.\-]+\.[A-Za-z]{2,3}$/', $email)) ) :
$err_email1 = "<div class = 'error'>Sorry, the email is not formatted properly</div>";
$formerrors = true;
However, it doesn't work when there is a period in the email. i.e. John.Smith#mydomain.com. It works fine with JohnSmith#mydomain.com so I know it's the second period.
How can I modify the code so it works with 2 period? I tried a number of variations but didn't have success.
If you have a good php regular expression site, I am all eyes.
Thanks

Try this
/^([a-z0-9_.-]+)#([\da-z.-]+).([a-z.]{2,6})$/

Related

The right way to do preg_match and preg_replace [duplicate]

This question already has answers here:
PHP: Best way to check if input is a valid number?
(6 answers)
Closed 2 years ago.
So I have been trying to bring back to life my very old website.
I started with replacing ereg with preg, but it's been a very long time since I have written any PHP.
At the moment I am stuck on this:
$_POST['amount'] = preg_replace("/[^0-9/]",'',$_POST['amount']);
$_POST['amount'] = round($_POST['amount']);
if (!preg_match('/[^0-9]/', $_POST['amount'])) {
echo "Invalid amount.";
}else {
echo "Passed";
}
I'm not entirely sure where I am going wrong. Should it be !preg_match or preg_match for example?
Edit:
$_POST['amount'] allows a user to enter a number and needs to replace anything else other than a number if attempted.
Not sure what the error is (i.e. what is the current output and what was expected). However, one mistake in the prompt is that the pattern for the first preg_replace appears to have a typo: /[^0-9/] should probably be /[^0-9]/
preg_replace("/[^0-9]/",'',$_POST['amount']);
The rest looks like correct syntax (i.e. preg_match returns 0 if there is no match).

How Can I hide part of my E-mail Address [duplicate]

This question already has answers here:
Partially hide email address in PHP
(19 answers)
Closed 5 years ago.
Good Morning Guys! I need to get an e-mail from my database, but I'd like to show it like that:
some*******#hotmail.com
I'd like to show the beginning of my e-mail and then everything after #.
I'm using the following code:
$user_email= "********".substr($linha3['user_email'], -8);
It's working, but sometimes It doesn't appear in a nice way:
********mail.com
May you help me with any solution?
Use RegEx to always match the e-mail username :
echo preg_replace('/.*#/', '***#', 'some_mail#somewhere.net');
$email = $linha3['user_email'];
$email= substr($email, 0, 3).'****'.substr($email, strpos($email, "#"));

How to get only mails from list? [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 6 years ago.
I have a html context that has usernames, mails and passwords in plain text format. There are two line breaks between information groups.
nickname
mail#example.com
pass
nickname2
mail2#example.com
pass2
I want to get all mails with a comma seperator and set them to a big string variable.
I tried many regexes but could not resolve the problem. Any suggestion to find the true regex will be appreciated.
The pattern:
.+#.+.\.\w+
Example:
https://regex101.com/r/nM1hK3/2
Edited
If .txt file is not too big (if you have enough memory and time):
$lines = file('mails.txt',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
function get_emails($val)
{
if (filter_var($val, FILTER_VALIDATE_EMAIL)) {
return $val;
}
}
$emails=array_filter($lines,"get_emails");
//print_r($emails);
$emails_var=join(",",$emails);
//echo $emails_var;
'mails.txt' -> path to your file, of course.

Eregi and preg_match replacement, big error on capcha and email validator [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 3 years ago.
I know there are a lot of questions related to this subject, but after some days of research I didn't find something that could help this particular case, so here it is:
I have replaced the deprecated eregi with preg_match in 2 files of my website, and now the capcha code gives an error on the registration page, even if the code is absolutely correct
On the registration page I have replaced this
function is_valid_username($username) {
if(!eregi("^[a-z0-9]*$", trim(str_replace(" ","",$username)))) {
return 0;
}
with this
if(!preg_match("^[a-z0-9]*$^", trim(str_replace(" ","",$username)))) {
return 0;
}
And in my second file I have replaced this:
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
$result = 0;
}
with this
if(!preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$^", $email)) {
$result = 0;
}
How can I resolve this issue?
eregi is case-insensitive, so you would need to add the i modifier to the end of your preg_match expression.
Also, ^ denotes the start of the input and you have used it as the delimiter.
So this should be more like the original:
#^[a-z0-9]*$#i
and
#^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$#i
By the way, I don't know what your captcha code requires exactly, but there are easier ways to verify an email address using filter_var().

preg_match email validation. (Adding hyphen to domain spot) [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP email validation
I'm working on this website, where I have to make a register form. I made it work, but then my friend told me, that some email domain names does have hyphens in them, like;
test#test-tester.com
Right now my code look like this:
preg_match("/^[a-zA-Z0-9._\-]\w+(\.\w+)*\#\w+(\.[a-zA-Z0-9._-]+)*\.[a-zA-Z.]{2,6}$/", $_POST["email"]
What I can see, this should work, but it still does not allow the hyphen.
Anyone know why?
Regex is always useful, but I prefer to use filter_var to validate emails
http://www.php.net/manual/en/filter.filters.validate.php
if(filter_var($email, FILTER_VALIDATE_EMAIL))
{
print('valid Email!');
}
else
{
print('invalid-email =(');
}

Categories