I want to sanitize the email a#$%#$#b##$#$2344324.com to a#b.com .
I tried and failed
echo filter_var("a#$%#$#b##$#$2344324.com", FILTER_SANITIZE_EMAIL); //result: a#$%#$#b##$#$2344324.com
I need to trim special characters in a email(sanitize to remove special characters). I used below code but I was unsuccessful.
$string = preg_replace("/^[a-zA-Z0-9._%+-]+#(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}$/", "", "a#$%#$#b##$#$2344324.com");
echo $string;//result: a#b.com -- unwanted characters trimmed here.
There is already a RFC-based solution here: http://fightingforalostcause.net/misc/2006/compare-email-regex.php
function is_valid_email_address($email){
$qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
$dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
$atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
'\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
$quoted_pair = '\\x5c[\\x00-\\x7f]';
$domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
$quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
$domain_ref = $atom;
$sub_domain = "($domain_ref|$domain_literal)";
$word = "($atom|$quoted_string)";
$domain = "$sub_domain(\\x2e$sub_domain)*";
$local_part = "$word(\\x2e$word)*";
$addr_spec = "$local_part\\x40$domain";
return preg_match("!^$addr_spec$!", $email) ? true : false;
}
Related
I have a file that i'm parsing And I'm trying to replace $mail["email_from"] = "test#example.com"; with $mail["email_from"] = request("email");,(meaning that I want to replace all the lines that has $mail["email_from"] at the begining an ; at the end) and here's my preg_replace:
$email = "$mail[\"email_from\"] = request(\"email\")";
$newcontent = preg_replace("/\$mail[\"email_from\"](.+);/",$email,$content);
What's the error in my code? and how to fix it? Much appreciated
DEMO
After using good quotes and escaping all chars you need, this works:
$email = '$mail["email_from"] = "test#example.com";';
$replacement = '$mail["email_from"] = request("email");';
$newContent = preg_replace('/\\$mail\\[\\"email_from\\"\\](.+);/i', $replacement, $email);
echo $newContent; //$mail["email_from"] = request("email");
$email = "$mail[\"email_from\"] = request(\"email\")";
^---double-quoted string
^^^^^---array reference
You probably need
$email = "\$mail[\"email_from\"] = request(\"email\")";
^--escape the $
Use ^ and $ to specify beginning and end of line. Special characters like $, [, and ] need to be escaped.
<?php
$content = '$mail["email_from"] = "test#example.com";';
$email = '$mail["email_from"] = request("email");';
$newcontent = preg_replace('/^\$mail\["email_from"\] =.+;$/',$email,$content);
echo $newcontent . "\n";
outputs:
$mail["email_from"] = request("email");
I want to block to use space. Where must i edit and what to write?
$k = "".$post['dname'.$i]."";
$name = preg_replace("/[^a-zA-Z0-9_-\s]/", "", $k);
$database->setVillageName($database->RemoveXSS($varray[$i]['wref']),$name);
If you only want to allow one space in your string, then you could use the or operator in a regex.
$str = 'some name';
if(preg_match('/^([\w]+|[\w]+ [\w]+)$/', $str, $matches))
{
echo 'success';
}
else
{
echo 'fail';
}
This code will succeed when there are 0 or 1 spaces in the string. Otherwise it will fail if it has more spaces.
You can play with the code: http://codepad.viper-7.com/yTtWz1
A preg_replace would of course be similar:
preg_replace('/^([\w]+|[\w]+ [\w]+)$/', "", $str)
Just remove the \s from the character class:
$name = preg_replace("/[^a-zA-Z0-9_-]/", "", $k);
or (shorter):
$name = preg_replace("/[^\w-]/", "", $k);
Edit
$k = "".$post['dname'.$i]."";
$name = preg_replace("/[^\w\s-]/", "", $k);
if (preg_match('/^\s+$/', $name) {
// error : $name mustn't be all spaces
// do appropriate stuff
}
$database->setVillageName($database->RemoveXSS($varray[$i]['wref']),$name);
In php, how can I check if a string has no characters at all.
Currently I do like below, and replace - with ' '. But if a search string contained all bad words, it'll leave me with ' '(3 blank spaces). The length will still show as 3 and it'll head off to the sql processor. Any method to check if a string has no characters or numbers at all?
$fetch = false;
#$strFromSearchBox = 'Why-you-foo-bar-I-ought-to-tar-you';
$strFromSearchBox = 'foo-bar-tar';
if(strlen($strFromSearchBox) >=2)
{
$newString = str_replace($theseWords,'',$strFromSearchBox);
$newString = str_replace('-',' ',$newString);
if(strlen($newString)>=2)
{
$fetch = true;
echo $newString;
}
}
if($fetch){echo 'True';}else{echo 'False';}
$fetch = false;
#$strFromSearchBox = 'Why-you-foo-bar-I-ought-to-tar-you';
$strFromSearchBox = 'foo-bar-tar';
if(strlen($strFromSearchBox) >=2)
{
$newString = str_replace($theseWords,'',$strFromSearchBox);
$newString = str_replace('-',' ',$newString);
$newString=trim($newString); //This will make the string 0 length if all are spaces
if(strlen($newString)>=2)
{
$fetch = true;
echo $newString;
}
}
if($fetch){echo 'True';}else{echo 'False';}
If you strip the leading and backmost spaces, the length will go down to 0 which you can easily turn into the $fetch boolean:
$fetch = (bool) strlen(trim($newString));
See trimDocs.
Use regex perhaps...
if (preg_match('/[^A-Za-z0-9]+/', $strFromSearchBox))
{
//is true that $strFromSearchBox contains letters and/or numbers
}
I'm practicing my beginner php skills and would like to know why this script always returns FALSE?
What am i doing wrong?
$namefields = '/[a-zA-Z\s]/';
$value = 'john';
if (!filter_var($value,FILTER_VALIDATE_REGEXP,$namefields)){
$message = 'wrong';
echo $message;
}else{
$message = 'correct';
echo $message;
}
The regexp should be in an options array.
$string = "Match this string";
var_dump(
filter_var(
$string,
FILTER_VALIDATE_REGEXP,
array(
"options" => array("regexp"=>"/^M(.*)/")
)
)
); // <-- look here
Also, the
$namefields = '/[a-zA-Z\s]/';
should be rather
$namefields = '/[a-zA-Z\s]*/'; // alpha, space or empty string
or
$namefields = '/[a-zA-Z\s]+/'; // alpha or spaces, at least 1 char
because with the first version I think you match only single-character strings
Basically what I want to do is display an email using javascript to bring the parts together and form a complete email address that cannot be visible by email harvesters.
I would like to take an email address eg info#thiscompany.com and break it to:
$variable1 = "info";
$variable2 = "thiscompany.com";
All this done in PHP.
Regards,
JB
list($variable1, $variable2) = explode('#','info#thiscompany.com');
$parts = explode("#", $email_address);
Assuming that $email_address = 'info#thiscompany.com' then $parts[0] == 'info' and $parts[1] == 'thiscompany.com'
You can use explode:
$email = 'info#thiscompany.com';
$arr = explode('#',$email);
$part1 = $arr[0]; // info
$part2 = $arr[1]; // thiscompany.com
$email = "info#thiscompany.com";
$parts = explode("#", $email);
Try this one before you roll your own (it does a lot more):
function hide_email($email)
{ $character_set = '+-.0123456789#ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
$key = str_shuffle($character_set); $cipher_text = ''; $id = 'e'.rand(1,999999999);
for ($i=0;$i<strlen($email);$i+=1) $cipher_text.= $key[strpos($character_set,$email[$i])];
$script = 'var a="'.$key.'";var b=a.split("").sort().join("");var c="'.$cipher_text.'";var d="";';
$script.= 'for(var e=0;e<c.length;e++)d+=b.charAt(a.indexOf(c.charAt(e)));';
$script.= 'document.getElementById("'.$id.'").innerHTML=""+d+""';
$script = "eval(\"".str_replace(array("\\",'"'),array("\\\\",'\"'), $script)."\")";
$script = '<script type="text/javascript">/*<![CDATA[*/'.$script.'/*]]>*/</script>';
return '<span id="'.$id.'">[javascript protected email address]</span>'.$script;
}
How about a function for parsing strings according to a given format: sscanf. For example:
sscanf('info#thiscompany.com', '%[^#]#%s', $variable1, $variable2);