I have used
if (!preg_match('/[a-z||0-9]#[a-z||0-9].[a-z]/', $email)) {
[PRINT ERROR]
}
&
if (!eregi( "^[0-9]+$", $email)) {
[PRINT ERROR]
}
&
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
[PRINT ERROR]
}
I have also tried taking out the ! and make it work backwards but for some reason NONE of those work to find out if it is valid. Any ideas why?...
I have it in an else if statement, Im not sure if that could be the cause..
I am using PHP
Try
'/[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}/'
...
if (!preg_match('/[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}/', strtoupper($email))) {
[PRINT ERROR]
}
As far as I can see, none of your regex expressions would match an email.
Try this from the Kohana source code:
function email($email)
{
return (bool) preg_match('/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+#(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD', (string) $email);
}
Check your php version. eregi is deprecated after 5.3.0. Also, the regex is not correct.
Try this (from wordpress):
// from wordpress code: wp-includes/formatting.php
function is_email($user_email)
{
$chars = "/^([a-z0-9+_]|\\-|\\.)+#(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
if (strpos($user_email, '#') !== false && strpos($user_email, '.') !== false)
{
if (preg_match($chars, $user_email)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
Related
I'm using this validator:
//validate postcode
function IsPostcode($postcode)
{
$postcode = strtoupper(str_replace(' ','',$postcode));
if(preg_match("/^[A-Z]{1,2}[0-9]{2,3}[A-Z]{2}$/",$postcode) || preg_match("/^[A-Z]{1,2}[0-9]{1}[A-Z]{1}[0-9]{1}[A-Z]{2}$/",$postcode) || preg_match("/^GIR0[A-Z]{2}$/",$postcode))
{
return true;
}
else
{
return false;
}
}
From this link.
But I want to be able to validate postcodes like ME20 and TN10 instead of a full blown ME20 4RN, TN0 4RN. This is the part of the postcode known as the 'outward code'.
Can someone help me out with the regex?
you can use my updated regex to solve you problem
it working from my end to validate UK zip code
<?php
function IsPostcode($postcode)
{
$postcode = strtoupper(str_replace(' ','',$postcode));
if(preg_match("/(^[A-Z]{1,2}[0-9R][0-9A-Z]?[\s]?[0-9][ABD-HJLNP-UW-Z]{2}$)/i",$postcode) || preg_match("/(^[A-Z]{1,2}[0-9R][0-9A-Z]$)/i",$postcode))
{
return true;
}
else
{
return false;
}
}
echo $result = IsPostcode('ME20');
?>
OUTPUT
1
Hope this will sure help you.
I am trying to make my php code psr-1 psr-2 compliant Don't i am getting some weird errors actually i am not understanding what exactly it want me to solve :(
Issue-1
Error: Opening parenthesis of a multi-line function call must be the last content on the line.
if (Configuration::updateValue('AV_GTC_CT_GT_DG_CT', $AV_GTC_CT) &&
Configuration::updateValue('AV_GTC_ST_CR_GP', $AV_GTC_ST) &&
Configuration::updateValue('AV_GTC_SD_NN_EA_AR_CN_AT_CT', $AV_GTC_SD) &&
Configuration::updateValue('AV_GTC_SD_NN_EA_AR_BK_CN', $AV_GTC_SD_NN_EA_AR_BK_CN) &&
Configuration::updateValue('AV_GTC_SW_GT_TO_CR_AT_BN_AT_OR_AS_PE',
$AV_GTC_SW_GT_TO_CR_AT_BN_AT_OR
) &&
Configuration::updateValue('AV_GTC_CN_CE_FR_LG_CR_CN', $AV_GTC_CN)
) {
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
Issue-2
Error:Expected "if (...) {\n"; found "if (...)\n {\n"
if (!$customer->isGuest())
{
return false;
}
Any Clue Guys?
Other code patches that are showing same errors
if (!$customer->isGuest()){
return false;
}
if (empty($password)){
$password = Tools::passwdGen();
}
if (empty($id_customer)||empty($id_guest)){
return false;
}
if (empty($id_guest) || empty($id_customer)){
return false;
}
Thanks!
Issue1:
&& should be on the next line
open ( and close ) should be in the same line for single line argument listing :)
can't format source here, external url: pastebin link
Issue2:
{ should be on the same line
if (!$customer->isGuest()) {
return false;
}
I'm working on implementing some geoIP functionality to redirect a user away from my .com site to the relevant country domain (.fr, .es, .co.uk ...etc).
I've the following in my index.php to check the users IP:
ini_set('display_errors', 1);
require_once("geoip.inc");
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
if($country_code == 'ES')
{
header('Location: https://www.testsite.es');
}
elseif($country_code == 'GB')
{
header('Location: https://www.testsite.co.uk');
}
elseif($country_code == 'FR')
{
header('Location: https://www.testsite.fr');
}
else {
header('Location: https://www.testsite.com/home');
}
When I check the $country_code variable it is an empty String and as a result the above fails and I always hit https://www.testsite.com/home...
I started delving into the code and noticed that first I call this method:
function geoip_country_code_by_addr($gi, $addr) {
if ($gi->databaseType == GEOIP_CITY_EDITION_REV1) {
$record = geoip_record_by_addr($gi, $addr);
if ($record !== false) {
return $record->country_code;
}
} else {
$country_id = geoip_country_id_by_addr($gi, $addr);
if ($country_id !== false) {
return $gi->GEOIP_COUNTRY_CODES[$country_id];
}
}
return false;
}
which calls:
function geoip_country_id_by_addr($gi, $addr) {
$ipnum = ip2long($addr);
return _geoip_seek_country($gi, $ipnum) - GEOIP_COUNTRY_BEGIN;
}
I can't figure out why it keeps failing and returning a '0'? I am using Maxminds geoip.inc php to check the country code.
I've checked that mbstring is enabled within my php.ini file and it is. For some reason it just doesn't find the Country code based on the IP I pass to it. Does anyone have any help in terms of what might be causing this?
just wanted to say that I've resolved the issue. A mistake on my part and probably a sign that I need a break!
Within geoip.inc.php supplied by Maxmind I was initially getting these errors:
Cannot redeclare geoip_country_code_by_name() in geoip.inc on line 438
In order to fix this I simply check if the method is defined and if not I use it as follows:
if (!function_exists('geoip_country_code_by_name')) {
function geoip_country_code_by_name($gi, $name) {
$country_id = geoip_country_id_by_name($gi, $name);
if ($country_id !== false) {
return $gi->GEOIP_COUNTRY_CODES[$country_id];
}
return false;
}
}
I unfortunately had a minor typo in the above code which prevented the code from executing properly hence returing 0 each and every time.
I am trying to validate email in php using ereg, where I am not allowed to enter more than two dots after # and it can't begin with any special character, how can I do it.
function chk($a)
{
$pattern = "^([A-Za-z0-9\.|-|_]{1,60})([#])";
$pattern .="([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,3})$";
if (!#ereg($pattern, $a))
return false;
else
return true;
}
Please don't roll your own email validation.
if(filter_var($email, FILTER_VALIDATE_EMAIL) === true){
return true;
} else {
return false;
}
preg_match("/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+#[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/",'test#test.co.in.');
function custom_email_confirmation_validation_filter( $your_email ) {
if(!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*#([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $your_email )){
return 'invalid';
}
if( substr_count($your_email, '.') > 3){
return 'invalid 1';
}
return 'valid';
}
echo custom_email_confirmation_validation_filter('golapk.kkk.khazi#gmail.com');
I am trying to read all lines from a file and than see if a given string contains any of these lines.
My code
$mails = file('blacklist.txt');
$email = "hendrik#anonbox.net";
$fail = false;
foreach($mails as $mail) {
if(strpos($email, $mail) > 0) {
$fail = true;
}
}
if($fail) {
echo "Fail";
} else {
echo "you can use that";
}
The blacklist.txt can be found here http://pastebin.com/aJyVkcNx.
I would expect strpos return a position for at least one string in the blacklist, but it does not. I am guessing that somehow I am generating not the kind of values within the $mails as I am expecting.
EDIT this is print_r($mails) http://pastebin.com/83ZqVwHx
EDIT2 some clarification: I want to see if a domain is within an email, even if the mail contains subdomain.domain.tld. And I tried to use !== false instead of my > 0 which yielded the same result.
You need to parse the email well since you're checking the domain of the email address if its inside the blacklist. Example:
$email = "hendrik#foo.anonbox.net";
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
preg_match('/#.*?([^.]+[.]\w{3}|[^.])$/', $email, $matches);
if(!empty($matches) && isset($matches[1])) {
$domain = $matches[1];
} else {
// not good email
exit;
}
// THIS IS FOR SAMPLES SAKE, i know youre using file()
$blacklist = explode("\n", file_get_contents('http://pastebin.com/raw.php?i=aJyVkcNx'));
foreach($blacklist as $email) {
if(stripos($email, $domain) !== false) {
echo 'you are blacklisted';
exit;
}
}
}
// his/her email is ok continue
strpos returns FALSE if the string was not found.'
Simply use this :
$fail = false;
foreach($mails as $mail) {
if(strpos($email, $mail) === false) {
$fail = true;
}
}
Or even better use this:
$blacklist = file_get_contents('blacklist.txt');
$email = "hendrik#anonbox.net";
if(strpos($email, $blacklist) === false){
echo "fail";
} else {
echo "This email is not blacklisted";
}
You have found the common pitfall with the strpos function. The return value of the strpos function refers to the position at which it found the string. In this instance, if the string begins at the first character, it will return 0. Note that 0 !== false.
The correct way to use the function is:
if(strpos($email, $mail) !== false){
// the string was found, potentially at position 0
}
However, this function may not be necessary at all; if you are simply checking if $mail is the same as $email, instead of seeing if the string exists within a larger string, then just use:
if($mail == $email){
// they are the same
}
Though you might still use foreach, that’s array reduce pattern:
function check_against($carry, $mail, $blacklisted) {
return $carry ||= strpos($mail, $blacklisted) !== false;
};
var_dump(array_reduce($mails, "check_against", $email_to_check));
Hope it helps.
Yet another way to solve this. Works fine:
$blacklist = file_get_contents('blacklist.txt');
$email = "hendrik#x.ip6.li";
$domain = substr(trim($email), strpos($email, '#')+1);
if(strpos($blacklist, $domain)){
echo "Your email has been blacklisted!";
}else{
echo "You are all good to go! not blacklisted :-)";
}
Goodluck!