how to get last values of email after the # - php

i am trying to determine the best way to determine whether an email address is an outlook or hotmail address.
i therefore need to collect the values after the #
i.e
testemail#outlook.com
caputure the #
however this will not work in all instance as
this email address is valid:
"foo\#bar"#iana.org
i read that a solution could be to explode it, i.e:
$string = "user#domain.com";
$explode = explode("#",$string);
array_pop($explode);
$newstring = join('#', $explode);
echo $newstring;
this solution seems bit long and only captures the first values
would really appreciate some help

if You explode This :
$string = "user#domain.com";
$explode = explode("#",$string);
it Will be :
$explode[0] = user
$explode[1] = domain.com

try to use array_reverse() ti pick the last value of email:
<?php
$email='exa#mple#hotmail.com';
$explode_email=explode('#',$email);
$reversed_array=array_reverse($explode_email);
$mailserver=explode('.',$reversed_array[0]);
echo $mailserver[0];
?>

You could always just keep it simple and test if either value exists in the string using strpos() or stripos().
if ( FALSE !== stripos($string, 'outlook') {
// outlook exists in the string
}
if ( FALSE !== stripos($string, 'hotmail') {
// hotmail exists in the string
}

I hope this will be easy for you to understand.
<?php
$emailAddress = 'mailbox#hotmail.com'; //Email Address
$emailStringArray = explode('#',$emailAddress); // take apart the email string.
$host = $emailStringArray[1]; //last string after # . $emailStringArray[0] = Mailbox & $emailStringArray[1] = host
if($host == "hotmail.com" || $host == "outlook.com"){
//matches to outlook.com or hotmail.com
}
else{
//Does not match to outlook.com or hotmail.com
}

I would recommend matching with a regular expression.
if (preg_match("/\#hotmail.com$/", $email)) {
echo "on hotmail";
} else if (preg_match("/\#outlook.com$/", $email)) {
echo "on outlook";
} else {
echo "different domain";
}
Additionally, if you want to capture full domain to variable, you can do it like this:
$matches = [];
if (preg_match("/^.*\#([\w\.]+)$/", $email, $matches)) {
echo "Domain: " . $matches[1];
} else {
echo "not a valid email address.";
}

Try this :
$emailAddress = 'example\#sometext\#someothertext#hotmail.com';
$explodedEmail = explode('#', $emailAddress);
$emailServerHostName = end($explodedEmail);
$emailServerNameExploded = explode('.', $emailServerHostName);
$emailServerName = $emailServerNameExploded[0];
echo $emailServerName;

Related

How to check subdomain with string is "us"

I have a sample code to check if the string subdomain is "us"
$string = "www.domain.com"; // 1
$string = "domain.com"; // 2
$string = "us.domain.com"; // 3
$string = "www.us.domain.com"; // 4
if (preg_match("/^(?=.{2})[a-z0-9]+(?:-[a-z0-9]+)*$/i", $_SERVER['SERVER_NAME'])) {
echo "$string include us";
} else {
echo "$string not include us";
}
But result error, how to fix it
If by "subdomain" you mean that the tld and the main domain are excluded, that means we'll have to check for the text "us" and two dots further in the text.
It will also match with sub-subdomains (Like us.subdomain.domain.us)
<?php
$domains = [
'www.domain.us',
'domain.us',
'domainuscomputers.com',
'us.domain.us',
'www.asuscomputers.usdomain.us',
'us.subdomain.domain.us',
];
$reg = '/us.*\..*\./';
foreach ($domains as $domain) {
$match = preg_match($reg, $domain) ? 'subdomain containing "us" found' : 'No match';
printf('domain %s : %s<br>', $domain, $match);
}
Note that the regular expression may be improved, but hey, it works !
In this specific case I don't see the need of using regular expressions.
I think a simple strpos will do the job.
Try this code:
$string = "www.domain.com"; // 1
$string = "domain.com"; // 2
$string = "us.domain.com"; // 3
$string = "www.us.domain.com"; // 4
if (strpos($string,'us.')) {
echo "$string include us";
} else {
echo "$string not include us";
}

Replace number and email with XXXX in a sentence using jQuery

I'd like to replace the numbers and email from the sentences.
Example
$message = "Hi this is john, my personal no is 1213456789 and my email address is john#gmail.com".
Output:
Hi this is john, my personal no is 1213456789 and my email address is john#gmail.com
I want the Output to be like this:
Output:
Hi this is john, my personal no is XXXXXXX789 and my email address is XXXX#gmail.com
But I'm currently getting like this :
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX#gmail.com
function which I'm using now
function numbers1($str)
{
if(($until = strpos($str, '#')) !== false)
{
$str = str_repeat('X', $until) . substr($str, $until);
}
}
Thanks in advance.
try preg_replace()
$str = "Hi this is john, my personal no is 1213456789 and my email address is john#gmail.com";
$replacements[1] = 'X';
$replacements[0] = 'XXXX#';
echo preg_replace(array('/[0-6]/', '/[ a-z]{0,4}+#/'), $replacements, $str);
output :- Hi this is john, my personal no is XXXXXXX789 and my email address is XXXX#gmail.com
$message = "Hi this is john, my personal no is 1213456789 and my email address is john#gmail.com";
$arr = explode(" ", $message);
foreach($arr as $key=>$val)
{
if(!preg_match ("/[^0-9]/", $val))
{
$val_new = "XXXXXXX".substr($val, -3);
$arr[$key] = $val_new;
}
else if(strpos($val, "#")>0)
{
$arr_email = explode("#", $val);
$arr_email[0] = "XXXX";
$val_new = implode("#", $arr_email);
$arr[$key] = $val_new;
}
}
$new_msg = implode(" ", $arr);
echo $new_msg;
UPDATE 2 :
$message = "Hi this is john, my personal no is 1213456789 and my email address is john#gmail.com";
$arr = explode(" ", $message);
foreach($arr as $key=>$val)
{
if(!preg_match ("/[^0-9]/", $val))
{
$val_new = "XXXXXXX".substr($val, -3);
$arr[$key] = $val_new;
}
else if(preg_match ("/^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$/", $val))
{
$arr_email = explode("#", $val);
$arr_email[0] = "XXXX";
$val_new = implode("#", $arr_email);
$arr[$key] = $val_new;
}
}
$new_msg = implode(" ", $arr);
echo $new_msg;
You're asking how to do this using jQuery, however your sample code is written in PHP. I'll answer your question with a PHP snippet as well.
The reason why your function isn't working is because you're replacing pretty much everything from the beginning of the string up to the position where the first '#' is found. Then you're adding as many 'X' as characters are until that position, followed by the rest of the string. To complicate things more, this won't work if two or more email addresses are found in your string.
This should do. You might need to tweak the regexes for both the phone numbers and the email addresses, though:
$message = "Hi this is john, my personal no is 1213456789 and my email address is john#gmail.com";
// get all phone numbers
preg_match('/\d{3,}/s', $message, $phones);
// get all email addresses
preg_match('/[a-z.-]+#[a-z.-]+/s', $message, $emails);
foreach ($phones as $phone)
{
$message = str_replace($phone, str_repeat('X', strlen($phone) - 3) . substr($phone, -3), $message);
}
foreach ($emails as $email)
{
$parts = explode('#', $email);
$message = str_replace($email, str_repeat('X', strlen($parts[0])) . '#' . $parts[1], $message);
}
// Hi this is john, my personal no is XXXXXXX789 and my email address is XXXX#gmail.com
echo $message;

How to find url using preg_match in php [duplicate]

I was wondering how I could check a string broken into an array against a preg_match to see if it started with www. I already have one that check for http://www.
function isValidURL($url)
{
return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}
$stringToArray = explode(" ",$_POST['text']);
foreach($stringToArray as $key=>$val){
$urlvalid = isValidURL($val);
if($urlvalid){
$_SESSION["messages"][] = "NO URLS ALLOWED!";
header("Location: http://www.domain.com/post/id/".$_POST['postID']);
exit();
}
}
Thanks!
Stefan
You want something like:
%^((https?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i
this is using the | to match either http:// or www at the beginning. I changed the delimiter to % to avoid clashing with the |
John Gruber of Daring Fireball has posted a very comprehensive regex for all types of URLs that may be of interest. You can find it here:
http://daringfireball.net/2010/07/improved_regex_for_matching_urls
I explode the string at first as the url might be half way through it e.g. hello how are you www.google.com
Explode the string and use a foreach statement.
Eg:
$string = "hello how are you www.google.com";
$string = explode(" ", $string);
foreach ($string as $word){
if ( (strpos($word, "http://") === 0) || (strpos($word, "www.") === 0) ){
// Code you want to excute if string is a link
}
}
Note you have to use the === operator because strpos can return, will return a 0 which will appear to be false.
I used this below which allows you to detect url's anywhere in a string. For my particular application it's a contact form to combat spam so no url's are allowed. Works very well.
Link to resource: https://css-tricks.com/snippets/php/find-urls-in-text-make-links/
My implementation;
<?php
// Validate message
if(isset($_POST['message']) && $_POST['message'] == 'Include your order number here if relevant...') {
$messageError = "Required";
} else {
$message = test_input($_POST["message"]);
}
if (strlen($message) > 1000) {
$messageError = "1000 chars max";
}
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
if (preg_match($reg_exUrl, $message)) {
$messageError = "Url's not allowed";
}
// Validate data
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
Try implode($myarray, '').strstr("www.")==0. That implodes your array into one string, then checks whether www. is at the beginning of the string (index 0).

Detecting a url using preg_match? without http:// in the string

I was wondering how I could check a string broken into an array against a preg_match to see if it started with www. I already have one that check for http://www.
function isValidURL($url)
{
return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}
$stringToArray = explode(" ",$_POST['text']);
foreach($stringToArray as $key=>$val){
$urlvalid = isValidURL($val);
if($urlvalid){
$_SESSION["messages"][] = "NO URLS ALLOWED!";
header("Location: http://www.domain.com/post/id/".$_POST['postID']);
exit();
}
}
Thanks!
Stefan
You want something like:
%^((https?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i
this is using the | to match either http:// or www at the beginning. I changed the delimiter to % to avoid clashing with the |
John Gruber of Daring Fireball has posted a very comprehensive regex for all types of URLs that may be of interest. You can find it here:
http://daringfireball.net/2010/07/improved_regex_for_matching_urls
I explode the string at first as the url might be half way through it e.g. hello how are you www.google.com
Explode the string and use a foreach statement.
Eg:
$string = "hello how are you www.google.com";
$string = explode(" ", $string);
foreach ($string as $word){
if ( (strpos($word, "http://") === 0) || (strpos($word, "www.") === 0) ){
// Code you want to excute if string is a link
}
}
Note you have to use the === operator because strpos can return, will return a 0 which will appear to be false.
I used this below which allows you to detect url's anywhere in a string. For my particular application it's a contact form to combat spam so no url's are allowed. Works very well.
Link to resource: https://css-tricks.com/snippets/php/find-urls-in-text-make-links/
My implementation;
<?php
// Validate message
if(isset($_POST['message']) && $_POST['message'] == 'Include your order number here if relevant...') {
$messageError = "Required";
} else {
$message = test_input($_POST["message"]);
}
if (strlen($message) > 1000) {
$messageError = "1000 chars max";
}
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
if (preg_match($reg_exUrl, $message)) {
$messageError = "Url's not allowed";
}
// Validate data
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
Try implode($myarray, '').strstr("www.")==0. That implodes your array into one string, then checks whether www. is at the beginning of the string (index 0).

How can I break an email address into different parts in php?

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);

Categories