How can I break an email address into different parts in php? - 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);

Related

How do I read / get contents of a text message body?

I'm going to use a SMS to Web Server, which will forward incoming text messages to my web server.
The message body will contain some short codes, a mobile number and a amount in a format. I need to store the details separately to variables.
Here's an example, what I will receive in my web server through POST method:
$sender = "9999912345";
$date = "2017-04-14 13:04:40";
$message = "RCG ARTL 9700012345 50";
I need to store the contents of $message separately to variables:
$cmd = "RCG";
$op = "ARTL";
$no = "9700012345";
$amt = "50";
Please help!
Please try this code:
$message = "RCG ARTL 9700012345 50";
$arr = explode(" ", $message);
$cmd = $arr[0];
$op = $arr[1];
$no = $arr[2];
$amt = $arr[3];
<?php
$message = "RCG ARTL 9700012345 50";
list($cmd, $op, $no, $amt) = explode(" ",$message);
you can test it.
Try this code, use explode function with space as the delimiter and then make sure exploded message (an array) is contains 4 key/value.
$cmd = '';
$op = '';
$no = '';
$amt = '';
$explodedMessage = explode(' ', $message);
if (count($explodedMessage)==4){
$cmd.=$explodedMessage[0];
$op.=$explodedMessage[1];
$no.=$explodedMessage[2];
$amt.=$explodedMessage[3];
}
You can try this.
$message = "RCG ARTL 9700012345 50";
list($cmd, $op, $no, $amount) = explode(' ', $message);
var_dump($cmd, $op, $no, $amount);
Output:
string(3) "RCG"
string(4) "ARTL"
string(10) "9700012345"
string(2) "50"
You can convert the $message into an array using explode function where the delimitre will be a space. and then can access it the way you want it.
$message = "RCG ARTL 9700012345 50";
$messageArray = explode(" ", $message);
$someText = $messageArray[0];

how to get last values of email after the #

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;

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 do I remove specific URL from user input?

I want remove some URL from user input.
Example user input
$input = 'http://www.yahoo.com/';
$input = 'http://yahoo.com/';
$input = 'www.yahoo.com';
$input = 'yahoo.com';
$input = 'http://answers.yahoo.com/question/index;_ylt=AhiI2Ax0jmujpU01wK7W4cLj1KIX;_ylv=3?qid=20110224130854AA9LGdy';
$input = 'yahoo';
$input = 'http://yahoo.com';
$input = 'I love http://yahoo.com';
output I love
Any domain with yahoo.com I want remove the output or return empty result. Maybe more than one domain. Let me know.
Try something like this:
$domains = array('yahoo.com', 'other-domain.org');
$domainsrgx = implode('|', array_map('preg_quote', $domains));
$filtered_userinput = preg_replace('#(^|\s+)(https?://)?([^/\s]*\.)?('.$domainsrgx.')(/[^\s]*)?(?=(\s|$))#is', '', $userinput);
This should remove everything from your example.
$data=str_replace('yahoo.com','',$data);
you can use an array to replace\remove multiple domains
$remove=array('yahoo.com','google.com');
$data=str_replace($remove,'',$data);
Ok. You can do
$rows = split("[\n|\r]", $input);
$output = "";
foreach($rows as $row){
if(strpos($row,"yahoo.com")){
continue;
}else{
$output = $row."\n";
}
}
I hope I'm not missing anything.

Extract 2 sets of numbers from a string using PHP's preg?

Here's some PHP code:
$myText = 'ABC #12345 (2009) XYZ';
$myNum1 = null;
$myNum2 = null;
How do I add the first set of numbers from $myText after the # in to $myNum1 and the second numbers from $myText that are in between the () in to $myNum2. How would I do that?
preg_match('/#(\d+).*\((\d+)\)/', $myText, $matches);
$myNum1 = $matches[1];
$myNum2 = $matches[2];
assuming you have something like:
" stuff ... #123123 stuff (456456)"
that will give you
$myNum1 = 123123
$myNum2 = 456456
If you have an input string of form "123#456", you can do
$tempArray = explode("#", $input);
if (sizeof($tempArray) != 2) {
echo "OH NO! Something bad happened!";
}
$value1 = intval($tempArray[0]);
$value2 = intval($tempArray[1]);
echo "Result: " . ($value1 + $value2);

Categories