Warning: preg_match() [function.preg-match]: Unknown modifier '{' i [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning: preg_match() [function.preg-match]: Unknown modifier '/'
I am having troubles with this code wich gives me the next error:
Warning: preg_match() [function.preg-match]: Unknown modifier '{' in /usr/home/anubis-cosmetics.com/web/includes/functions/functions_email.php on line 568
Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in /usr/home/anubis-cosmetics.com/web/includes/functions/functions_email.php on line 586
Line 568:
if email domain part is an IP address, check each part for a value under 256
if (!preg_match ($valid_ip_form, $domain)) {
$digit = explode( ".", $domain );
for($i=0; $i<4; $i++) {
if ($digit[$i] > 255) {
$valid_address = false;
return $valid_address;
exit;
}
Line 586:
if (!preg_match($space_check, $email)) { // trap for spaces in
if (!preg_match ($valid_email_pattern, $email)) { // validate against valid email patterns
$valid_address = true;
} else {
$valid_address = false;
return $valid_address;
exit;
}
}
return $valid_address;
}
?>
I don't know how to do it, can someone help me with this?
EDIT/////
I've tried to change the delimiters for this: # --->
// if email domain part is an IP address, check each part for a value under 256
if (!preg_match ($valid_ip_form, $domain))#
$digit = explode( ".", $domain );
And the other one for this:
if (!preg_match($space_check, $email)) { // trap for spaces in
if (!preg_match ($valid_email_pattern, $email)) { // validate against valid email patterns
$valid_address = true;
} else {
$valid_address = false;
return $valid_address;
exit;
}
}
return $valid_address;#
And still without work... Can someone be more especific (showing me some example inline) about the problem? Thanks!

The problem is with your $valid_ip_form respectively $space_check. They will be invalid php PCRE expressions.
I guess you're missing opening and closing delimiters and your expression looks like:
^[0-9]{3}$
Instead of:
~^[0-9]{3}$~
/^[0-9]{3}$/
#^[0-9]{3}$#
or whatever else you like.
Post them if you want more info

Related

preg_match returns true for an invalid phone number

Below is my code to verify a phone number. The issue is with an invalid phone number it still returns true. How can fix this?
$pattern = '/\(?[\d]{3}\)?\s?\-?[\d]{3}\s?\-?[\d]{4}/';
$str = array(
"702 622 0277",
"(702)622-0277",
"702-622-0277",
"(702) 622 0277",
"1234", // false
"7026220277",
"+17026220277", // should be false
"+17777036880277" // should be false
);
foreach($str as $each) {
if(preg_match($pattern, $each, $matches)) {
echo "$each is valid <br>";
} else {
echo "$each is invalid <br>";
}
}
Output:
702 622 0277 is valid
(702)622-0277 is valid
702-622-0277 is valid
(702) 622 0277 is valid
1234 is invalid
7026220277 is valid
+17026220277 is valid
+17777036880277 is valid
The problem here is that your string can match anything in the middle. So +17026220277 matches because 7026220277 is at the end
Try this pattern
$pattern = '/^\(?[\d]{3}\)?\s?\-?[\d]{3}\s?\-?[\d]{4}$/';
^ denotes the start of the string and $ denotes the end

function eregi() is deprecated in email validation [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 9 years ago.
Hi ı know that we do not eregi but preg_match but when ı change only eregi code it doesnt work, how can ı change the code below please just a little help, ı am a newbie
function verify_valid_email($emailtocheck)
{
$eregicheck = "^([-!#\$%&'*+./0-9=?A-Z^_`a-z{|}~])+#([-!#\$%&'*+/0-9=?A-Z^_`a-z{|}~]+\\.)+[a-zA-Z]{2,4}\$";
return eregi($eregicheck, $emailtocheck);
}
function verify_email_unique($emailtocheck)
{
global $config,$conn;
$query = "select count(*) as total from members where email='".mysql_real_escape_string($emailtocheck)."' limit 1";
$executequery = $conn->execute($query);
$totalemails = $executequery->fields[total];
if ($totalemails >= 1)
{
return false;
}
else
{
return true;
}
}
If you need to validate e-mail addresses, you can look at this page which provides a working example using only filter_var() :
if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
echo "This ($email_a) email address is considered valid.";
};
So in your code, you should just drop all the regex/eregi stuff and use this instead :
return filter_var($emailtocheck, FILTER_VALIDATE_EMAIL);
If you want to do it this way, you can base yourself on the following methods:
<?php
$email = \"abc123#somewhere\"; // Invalid email address
//$email = \"somebody#somesite.com\"; // Valid email address
// Set up regular expression strings to evaluate the value of email variable against
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
// Run the preg_match() function on regex against the email address
if (preg_match($regex, $email)) {
echo $email . \" is a valid email. We can accept it.\";
} else {
echo $email . \" is an invalid email. Please try again.\";
}
?>
or:
$string = "$emailtocheck";
if (preg_match(
'/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\#[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/',
$string)) {
echo "Successful.";
}
or:
<?php
$email = "abc123#sdsd.com";
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
if (preg_match($regex, $email)) {
echo $email . " is a valid email. We can accept it.";
} else {
echo $email . " is an invalid email. Please try again.";
}
?>
Source: https://stackoverflow.com/a/13719991/1415724
or:
<?php
// check e-mail address
// display success or failure message
if (!preg_match("/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*#([a-zA-Z0-9_-
])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/", $_POST['e-mail'])) {
die("Invalid e-mail address");
}
echo "Valid e-mail address, processing...";
?>
Source: http://www.techrepublic.com/article/regular-expression-engine-simplifies-e-mail-validation-in-php/
Plus, you can try what André Daniel wrote as an answer as well. You have many choices.

Make sure username contains only: A-Z, a-z, ., _, -, 0-9 (no comma) PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
regular expression for letters, numbers and - _
I am creating a signup form in PHP and need to ensure that user's usernames don't contain unwanted characters. Is there anyway I can create a function that returns true for A-Z a-z 0-9 - . _.
Also I do not want the user's emails to be from yahoo as for some reason they reject the confirmation emails sent. Along with __FILTER_VALIDATE_EMAIL__what would I need to add?
PS: is there anything wrong with the characters I have mentioned above? I have noted that gmail doesn't allow -_ only . and YouTube only alpha-numeric characters.
Edited to use \w instead of a-zA-Z0-9_
if(preg_match("/[^\w-.]/", $user)){
// invalid character
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL) || strstr($email,'#yahoo.com')) {
// either invalid email, or it contains in #yahoo.com
}
if(preg_match("/[^-A-Za-z0-9._ ]/", $userName)){
// there are one or more of the forbidden characters (the set of which is unknown)
}
<?php
// The validator class
class Validator
{
public function isValidUsername($username)
{
if(preg_match('/^[a-zA-Z0-9_\-\.]+$/', $username)) {
return true;
}
return false;
}
public function isYahooMail($mail) {
if(preg_match('/^[a-zA-Z0-9_\-\.]+#yahoo.com$/', $mail)) {
return true;
}
return false;
}
}
// The way to use this class
$username = "otporan_123";
$email = "otporan#gmail.com";
$badUsername = "otporan*bad";
$yahooEmail = "otporan#yahoo.com";
$validator = new Validator();
var_export($validator->isValidUsername($username));
echo "<br />";
var_export($validator->isValidUsername($badUsername));
echo "<br />";
var_export($validator->isYahooMail($email));
echo "<br />";
var_export($validator->isYahooMail($yahooEmail));
echo "<br />";
?>
This code would return:
true
false
false
true
This is a class, but you can see whats going on in methods and write your own functions if you like procedural code :)
Hope this helps!
if (!preg_match('/\w\-/', $username) {
//throw error
}

Why won't preg_match work?

Ever since "ereg" became depreciated, I began to use "preg_match". Unfortunately in my code, it doesn't accept my valid e-mail address. I am certain that this Regular Expression i'm using is working, but what I'm looking for is an alternative in doing this function or to point out what I'm doing wrong.
Here's my function:
function validate($email){
$regex = "^((([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|/|=|\?|\^|_|`|\{|\||\}|~)+(\.([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|/|=|\?|\^|_|`|\{|\||\}|~)+)*)#((((([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.))*([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.(af|ax|al|dz|as|ad|ao|ai|aq|ag|ar|am|aw|au|at|az|bs|bh|bd|bb|by|be|bz|bj|bm|bt|bo|ba|bw|bv|br|io|bn|bg|bf|bi|kh|cm|ca|cv|ky|cf|td|cl|cn|cx|cc|co|km|cg|cd|ck|cr|ci|hr|cu|cy|cz|dk|dj|dm|do|ec|eg|sv|gq|er|ee|et|fk|fo|fj|fi|fr|gf|pf|tf|ga|gm|ge|de|gh|gi|gr|gl|gd|gp|gu|gt| gg|gn|gw|gy|ht|hm|va|hn|hk|hu|is|in|id|ir|iq|ie|im|il|it|jm|jp|je|jo|kz|ke|ki|kp|kr|kw|kg|la|lv|lb|ls|lr|ly|li|lt|lu|mo|mk|mg|mw|my|mv|ml|mt|mh|mq|mr|mu|yt|mx|fm|md|mc|mn|ms|ma|mz|mm|na|nr|np|nl|an|nc|nz|ni|ne|ng|nu|nf|mp|no|om|pk|pw|ps|pa|pg|py|pe|ph|pn|pl|pt|pr|qa|re|ro|ru|rw|sh|kn|lc|pm|vc|ws|sm|st|sa|sn|cs|sc|sl|sg|sk|si|sb|so|za|gs|es|lk|sd|sr|sj|sz|se|ch|sy|tw|tj|tz|th|tl|tg|tk|to|tt|tn|tr|tm|tc|tv|ug|ua|ae|gb|us|um|uy|uz|vu|ve|vn|vg|vi|wf|eh|ye|zm|zw|com|edu|gov|int|mil|net|org|biz|info|name|pro|aero|coop|museum|arpa))|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$";
if (preg_match($regex, $email)) {
return true;
} else {
return false;
}
}
and here's my code to react with the function.
if (validate($email) == false){
$_SESSION['error'] = "You have an invalid email!<br /><br />";
header("Location: contact.php");
die;
}
As I run this code, it shows this error message:
Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in C:\xampp\htdocs\2012\Next\inc\functions.php on line 11
Your regex pattern need delimiters:
function validate($email){
$regex = "/...your pattern.../";
if (preg_match($regex, $email)) {
return true;
} else {
return false;
}
}
EDIT: AND then you need to escape the delimiters characters in your regex, if present.
Using / as delimiter, you got two in your pattern:
$regex = "/^((([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+(\.([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+)*)#((((([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.))*([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.(af|ax|al|dz|as|ad|ao|ai|aq|ag|ar|am|aw|au|at|az|bs|bh|bd|bb|by|be|bz|bj|bm|bt|bo|ba|bw|bv|br|io|bn|bg|bf|bi|kh|cm|ca|cv|ky|cf|td|cl|cn|cx|cc|co|km|cg|cd|ck|cr|ci|hr|cu|cy|cz|dk|dj|dm|do|ec|eg|sv|gq|er|ee|et|fk|fo|fj|fi|fr|gf|pf|tf|ga|gm|ge|de|gh|gi|gr|gl|gd|gp|gu|gt| gg|gn|gw|gy|ht|hm|va|hn|hk|hu|is|in|id|ir|iq|ie|im|il|it|jm|jp|je|jo|kz|ke|ki|kp|kr|kw|kg|la|lv|lb|ls|lr|ly|li|lt|lu|mo|mk|mg|mw|my|mv|ml|mt|mh|mq|mr|mu|yt|mx|fm|md|mc|mn|ms|ma|mz|mm|na|nr|np|nl|an|nc|nz|ni|ne|ng|nu|nf|mp|no|om|pk|pw|ps|pa|pg|py|pe|ph|pn|pl|pt|pr|qa|re|ro|ru|rw|sh|kn|lc|pm|vc|ws|sm|st|sa|sn|cs|sc|sl|sg|sk|si|sb|so|za|gs|es|lk|sd|sr|sj|sz|se|ch|sy|tw|tj|tz|th|tl|tg|tk|to|tt|tn|tr|tm|tc|tv|ug|ua|ae|gb|us|um|uy|uz|vu|ve|vn|vg|vi|wf|eh|ye|zm|zw|com|edu|gov|int|mil|net|org|biz|info|name|pro|aero|coop|museum|arpa))|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$/";
EDIT 2:
about the error you were getting
Warning: preg_match() [function.preg-match]: No ending delimiter '^' found ...
it found the ^ character as first character in your pattern, taking it as a delimiter, and complained because it did not find a matching ^ delimiter ending the pattern.
You can do this without preg_xx altogether:
function validate($email)
{
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
if (false === validate($email)) {
// invalid email given
}
It uses filter_var() together with FILTER_VALIDATE_EMAIL

Having a problem validating email address in PHP with preg_match

Here's the code:
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!preg_match("/^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
Here's the error msg:
Warning: preg_match() [function.preg-match]: No ending delimiter '/' found in /home/bigsilkd/public_html/UBA/join.php on line 22
It's exactly what it says:
preg_match("/^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$"
should be
preg_match("/^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$/"
^
|
This was missing ---/
You shouldn't use regular expressions for validating emails. For example your regex wouldn't allow my email address +#example.org, which is a normal and valid email. Save my email! It's dying out, because of bad form validation! Use filter_var!
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
// email is valid
}

Categories