I have strings where unfortunately phone number and email are concatenated like this:
$phone_email = "617.651.3123mya123#some-site.com";
I'd like to be able to split between the last number and first letter.
Desired output
$phone = "617.651.3123";
$email = "mya123#some-site.com";
I'm using php but hopefully the strategy would be straightforward in any language.
EDIT
I've tried many things including trying to simply grab the email by removing the digits. $email = preg_replace('#^\d+#', '', $phone_email); That results in removing only the 617 ignoring the .
As others have pointed out, splitting between "the last number and first letter" may not be a good strategy, since email addresses can start with numbers. That said, I believe this does roughly what you asked for:
$phone_email = "617.651.3123mya123#some-site.com";
$matches = [];
preg_match("/([^a-zA-Z]*)(.*)/", $phone_email, $matches);
$phone = $matches[1];
$email = $matches[2];
echo "Phone: $phone\n";
echo "Email: $email\n";
// Output:
// Phone: 617.651.3123
// Email: mya123#some-site.com
Simple solution is:
$phone_email = "617.651.3123mya123#some-site.com";
$ms = array();
preg_match("/(.*\d)([a-z].*)/", $phone_email, $ms);
print_r($ms);
Of course cases when email starts with a number are not considered.
split after digits and points repetitions
print_r(preg_split('/(\d+\.?)+\K/', $phone_email,2));
demo
Related
Okey so i know that i can remove letters and numbers from a string by using preg_replace. but here is what im trying to do.
Im using a wordpress plugin called Engage forms, that stores submitted data in a mySQL database.
The tricky part is that the data that's being submitted is not stored cleanly for some reason.
(reason being that the devs that made the plugin with some purpurs for this and i cant figuer out how they print out without the unknown numbers and letters.)
Here is what i mean:
I submit the data
Firstname: Peter
Lastname: Stormare
Email: storm#ren.com
Phone: 0736997385
Here is how the data inside the mySQL database looks and how its being printed out on the webpage:
Very messy
a:4:{s:18:"firstname25601459863853";s:5:"Peter";s:23:"lastnamn77151459863853";s:8:"Stormare";s:19:"email58511459863853";s:13:"Storm#ren.com";s:21:"phone22101459863853";s:10:"0736997385";}
My attempt at cleaning up the data
So ive tried using my little knowledge and take away as much as i can with:
$patterns = array();
$patterns[0] = '/[{}]/';
$patterns[1] = '/[a-x]:[0-9]/';
$patterns[2] = '/[0-9]:/';
$patterns[3] = '/[a-x]:/';
$patterns[4] = '/[":"]/';
$patterns[5] = '/;/';
$patterns[6] = '/namn[1]/';
$replacements = array();
$replacements[2] = '';
$replacements[1] = '';
$replacements[0] = '';
$replacements[3] = '';
$replacements[4] = ' ';
$replacements[10] = ''
echo preg_replace($patterns, $replacements, $leadslist);
Which should output this now:
firstname25601459863853 Peter lastname77151459863853 Stormare email58511459863853 Storm#ren.com phone22101459863853 0736997385
The Big Question
Is there anyway i can now target the random numbers that follow firstname and the rest, without deleting the phone numbers? (the example phone number is 0736997385.
Possible solution
The best solution atm was submitted by user: Pei-turn.
He showed me how to remove any number bigger then 10 digits. Using this pattern in Preg_replace()
$patterns[3] = '/[0-9]{0,9}[0-9]{11,}/';
This works very well. Only thing im worried about is if some data might get removed due to weird future form submissions.
A phone number has 10 digits, try to remove all numbersequences shorter than 10 and longer then 10
[0-9]{0,9}
[0-9]{11,}
I am having a problem with regular expressions at the moment.
What I'm trying to do is that for each line through the iteration, it checks for this type of pattern: Lastname, Firstname
If it finds the name, then it will take the first letter of the first name, and the first six letters of the lastname and form it as an email.
I have the following:
$checklast = "[A-z],";
$checkfirst = "[A-z]";
if (ereg($checklast, $parts[1])||ereg($checkfirst, $parts[2])){
$first = preg_replace($checkfirst, $checkfirst{1,1}, $parts[2]);
print "<a href='mailto:$first.$last#email.com;'> $parts[$i] </a>";
}
This one obviously broke the code. But I was initially attempting to find only the first letter of the firstname and then after that the first six letters of the lastname followed by the #email.com This didn't work out too well. I'm not sure what to do at this point.
Any help is much appreciated.
How about something like this:
$name = 'Smith, John';
$email = preg_replace('/([a-z]{1,6})[a-z]*?,[\\s]([a-z])[a-z]*/i',
'\\2.\\1#email.com', $name);
echo $email; // J.Smith#email.com
Cheers
I've got a phone number input field, which allows a user to add a phone number in whatever format they want (555-555-5555, (555) 555 - 5555, etc).
Since it a phone number field only, I can ignore everything but the numbers in the field.
I'm currently using the following code. It extracts all the numbers, but the issue is that they are not in order - it's in a jumbled order.
How do I extract the numbers in the order that they appear in the original string?
preg_match_all('/\d+/', $Phone, $matches);
$Phone = implode('', $matches[0]);
Edit: They are actually not in a jumbled order from this function - I was inserting the numbers into a int(10) database field, which caused the jumbling. But, the answers below are still a more efficient way of accomplishing my goal.
Use preg_replace to remove any non-digits:
$numbers = preg_replace('/[^\d]/','',$Phone);
Note: '[^\d]' can be replaced with '\D' (safe in non-unicode mode).
$Phone = preg_replace('/[^\d]/', '', $Phone);
Why not just replace everything in the string that is not a digit?
$number = preg_replace("/[^0-9]/", '', $Phone);
Try it
if (!preg_match("/^[0-9\-]*$/",$Phone)) {
echo "Only Numeric and strip (-)";
}
Example:
Good: 0877-9320-9356
Failed: 0877 9320 9356 or 087793209356
I have a string as
$email_string='Aslam Doctor <aslam.doctor#gmail.com>';
From which I want to extract Name & Email using PHP? so that I can get
$email='aslam.doctor#gmail.com';
$name='Aslam Doctor'
Thanks in advance.
As much as people will probably recommend regular expression I'd say use explode().
Explode splits the string up in several substrings using any delimiter.
In this case I use ' <' as a delimiter to immediately strip the whitespace between the name and e-mail.
$split = explode(' <', $email_string);
$name = $split[0];
$email = rtrim($split[1], '>');
rtrim() will trim the '>' character from the end of the string.
Using explode + list:
$email_string = 'Aslam Doctor <aslam.doctor#gmail.com>';
list($name, $email) = explode(' <', trim($email_string, '> '));
If you can use the IMAP extension, the imap_rfc822_parse_adrlist function is all you need.
/via https://stackoverflow.com/a/3638433/204774
text variable have one paragraph. two emails are included there. using extract_emails_from_string() function we extracts those mails from that paragraph.
preg_match_all function will return all matching strings with the regular expression from inputs.
function extract_emails_from_string($string){
preg_match_all("/[\._a-zA-Z0-9-]+#[\._a-zA-Z0-9-]+/i", $string, $matches);
return $matches[0];
}
$text = "Please be sure to answer the Please arun1#email.com be sure to answer the Please be sure to answer the Please be sure to answer the Please be sure to answer the Please be sure to answer the Please be sure to answer the arun#email.com";
$emails = extract_emails_from_string($text);
print(implode("\n", $emails));
This is what I use - works for email addresses with and without the angle bracket formatting. Because we are searching from right to left, this also works for those weird instances where the name segment actually contains the < character:
$email = 'Aslam Doctor <aslam.doctor#gmail.com>';
$address = trim(substr($email, strrpos($email, '<')), '<>');
I have an email address that could either be
$email = "x#example.com"; or $email="Johnny <x#example.com>"
I want to get
$handle = "x"; for either version of the $email.
How can this be done in PHP (assuming regex). I'm not so good at regex.
Thanks in advance
Use the regex <?([^<]+?)# then get the result from $matches[1].
Here's what it does:
<? matches an optional <.
[^<]+? does a non-greedy match of one or more characters that are not ^ or <.
# matches the # in the email address.
A non-greedy match makes the resulting match the shortest necessary for the regex to match. This prevents running past the #.
Rubular: http://www.rubular.com/r/bntNa8YVZt
Here is a complete PHP solution based on marcog's answer
function extract_email($email_string) {
preg_match("/<?([^<]+?)#([^>]+?)>?$/", $email_string, $matches);
return $matches[1] . "#" . $matches[2];
}
echo extract_email("ice.cream.bob#gmail.com"); // outputs ice.cream.bob#gmail.com
echo extract_email("Ice Cream Bob <ice.cream.bob#gmail.com>"); // outputs ice.cream.bob#gmail.com
Just search the string using this basic email-finding regex: \b[A-Z0-9._%+-]+#[A-Z0-9.-]+.[A-Z]{2,4}\b
It will match any email in any text, and in your first string it will match the whole string, and in the second, only the part of the string that is e-mail.
To quickly learn regexp this is the best place: http://www.regular-expressions.info
$email = 'x#gmail.com';
preg_match('/([a-zA-Z0-9\-\._\+]+#[a-z0-9A-Z\-\._]+\.[a-zA-Z]+)/', $email, $regex);
$handle = array_shift(explode('#', $regex[1]));
Try that (Not tested)