-4

I have many SMS like this

I got a new mobile for Rs.18000 and my new mobile number is 9097123456, my landline number is 040-201234 and my email id is abcd@gmail.com -

Now I want to retrieve only the phone numbers and email id from the entire msg string and store in separate string variables. How to do this?

juergen d
  • 201,996
  • 37
  • 293
  • 362
xyzandroid
  • 139
  • 1
  • 3
  • 9

5 Answers5

2

Use regular expressions. There are plenty of resources that will provide you with expressions matching phone numbers, emails etc.

Bostone
  • 36,858
  • 39
  • 167
  • 227
0

Your best bet will be Regular Expressions. You can take a look at a tutorial here.

Basically, you will need to use groups to process your SMS content. Once the SMS message has been parsed by the regular expression, you can then extract the hits by accessing the groups.

So for instance, you can use a regular expression similar to the one provided here to extract an email id. Something like (\d{3}-\d{6}) will match any 3 numbers, followed by a dash which are then followed by another 6 numbers. The round brackets denote a regex group.

npinti
  • 51,780
  • 5
  • 72
  • 96
0

Look into Regular Expressions.

See here: http://komunitasweb.com/2009/03/10-practical-php-regular-expression-recipes/

You are given several methods for extracting phone numbers and emails from a string, with PHP and regexpressions.

$email = "test@example.com";
if (preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$email)) {
    echo "Your email is ok.";
} else {
    echo "Wrong email address format";
}

You can modify that to pull the email address out with a while loop, and store in an array with a $i++ to denote where it will be saved to. For the phone numbers, use this:

$phone = "(021)423-2323";
if (preg_match('/\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}/x', $phone)) {
    echo "Your phone number is ok.";
} else {
    echo "Wrong phone number.";
}
ionFish
  • 1,004
  • 1
  • 8
  • 20
0

I think you should use the Regular Expression for this.

Take each work and check for the email and land line number with their respective RedEx.

ALGO

for each word in SMS
    if email-RegEx matches word
        save email
    else if landLineNumber-RegEx matches word
        save landLineNumber
    end if
end for

return email,landLineNumber
Talha Ahmed Khan
  • 15,043
  • 10
  • 42
  • 49
0

you can use this purpose regular expression like here is regular expression to match the email

^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)@ [A-Za-z0-9]+(\.[A-Za-z0-9]+)(\.[A-Za-z]{2,})$

explanation of this regular expression

visit following link Regular Expression

Mahmood
  • 52
  • 1
  • 6