0

I think I'm going about this the wrong way, but here's what I'm doing.

I have a string passed to me from my PBX, which I'm attempting to grab only the phone number, and even the text inside the double quotes, if they exist.

<?php
  $string = '"John Smith" <sip:[email protected]:5060;user=phone>';
?>

I'm positive using a form of regex will be better to grab the number and name in separate variables, but since I'm not too good at regex yet, I'm resorting to explode()

I'm not doing too well with it, and it's getting messing with exploding the string at the @ sign, then at the :. This leaves the name.

How would I strip out the number after the <sip: before the @?

1
  • is double-quoted part optional, or does format always stay the same? I mean, immediate solution will be something like /"([^"])+"\D+(\d+)/. This regex can (and should) be improved (adding <sip:(\d+)@ part check instead of simple (\d+), for example) if the strings you're working with can be of different format.
    – raina77ow
    Commented Jul 16, 2013 at 7:23

5 Answers 5

1

First thing you may want to do is to split (using regexp) into part before and after <sip:

^(.*?)\s*<sip\:(.+)>\s*$

\s* means to skip any white spaces if there are any.

Matching phone number is:\d+ or [0-9]{1,} or whichever combo you like.

Matching the contents of "" would be trivial if you can rely that they are always there: \s*"([^"]+)"\s* or little worse if they are just optional: \s*(?:"([^"]+)"\s*)?

So lets put it all together:

$regex = '~^\s*(?:"([^"]+)"\s*)?<sip:(\d+)@.+>\s*~i';
$matches = array();
if( preg_match($regex, $string, $matches)){
    list(, $name, $phone) = $matches;
}
2
  • +1 for explaining how the regex works. (I'm assuming (.?*) was a typo and you really meant (.*?)). Also, <, : and @ have no special meaning, so you don't have to escape them. That's especially important in the case of the angle brackets because some regex flavors use \< and \> as word boundaries.
    – Alan Moore
    Commented Jul 16, 2013 at 8:13
  • @AlanMoore thanks for notes. (.?*) was indeed a typo and I've removed obsolete escapes.
    – Vyktor
    Commented Jul 16, 2013 at 9:26
0

Here's how to do it with regexp:

preg_match('/(?:"([^"]*)")?\s*<.*sip:(\d+\)/', $string, $match);
$name = $match[1];
$phone = $match[2];
0
if (preg_match('/^(?:\"([^\"]+)\" )?\<sip\:(\d+)\@/i', $string, $matches))
    list(, $name, $phone) = $matches;
0
<sip:([0-9]{1,})@

is the regex you need

preg_match('/".*?" <sip:([0-9]+)@/', $string, $matches); 
//matches[1] contains the name
//matches[2] contains the number
0
-1
check this

<?php

  $string = '"John Smith" <sip:[email protected]:5060;user=phone>';

  $first = explode("<sip:", $string);

  $second =  explode("@", $first[1]);

  echo $second[0];
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.