Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP从字符串中剥离特定文本_Php_Regex - Fatal编程技术网

PHP从字符串中剥离特定文本

PHP从字符串中剥离特定文本,php,regex,Php,Regex,我想我走错了方向,但我现在要做的是 我有一个从我的PBX传给我的字符串,我试图只获取电话号码, 甚至双引号内的文本,如果它们存在的话 <?php $string = '"John Smith" <sip:15558740205@pbx.domain.co:5060;user=phone>'; ?> 我肯定使用正则表达式形式会更好地在单独的变量中获取数字和名称,但由于我还不太擅长正则表达式,所以我选择使用explode() 我做得不太好,它在@符号处爆炸字符串,然

我想我走错了方向,但我现在要做的是

我有一个从我的PBX传给我的字符串,我试图只获取电话号码, 甚至双引号内的文本,如果它们存在的话

<?php
  $string = '"John Smith" <sip:15558740205@pbx.domain.co:5060;user=phone>';
?>

我肯定使用正则表达式形式会更好地在单独的变量中获取数字和名称,但由于我还不太擅长正则表达式,所以我选择使用
explode()

我做得不太好,它在
@
符号处爆炸字符串,然后在
处爆炸字符串。这留下了名字

检查此项后,我如何去掉数字

以下是如何使用regexp执行此操作:

preg_match('/(?:"([^"]*)")?\s*<.*sip:(\d+\)/', $string, $match);
$name = $match[1];
$phone = $match[2];

preg\u match('/(?:“([^”]*)”)\s*
if(preg\u match('/^(?:“([^”]+)\”)\”)\您可能要做的第一件事是将(使用regexp)拆分为
之前和之后的部分是双引号部分可选的,还是格式总是保持不变?我的意思是,立即的解决方案将类似于
/“([^”+)+”\D+/
。这个正则表达式可以(并且应该)改进(添加
+1来解释正则表达式的工作原理。(我假设
(.?*)
是一个打字错误,你的意思是
(.*?)
)。另外,
@alanmore感谢你的注释。
(.?*)
确实是一个打字错误,我已经删除了过时的转义符。
preg_match('/(?:"([^"]*)")?\s*<.*sip:(\d+\)/', $string, $match);
$name = $match[1];
$phone = $match[2];
<sip:([0-9]{1,})@
preg_match('/".*?" <sip:([0-9]+)@/', $string, $matches); 
//matches[1] contains the name
//matches[2] contains the number
if (preg_match('/^(?:\"([^\"]+)\" )?\<sip\:(\d+)\@/i', $string, $matches))
    list(, $name, $phone) = $matches;
^(.*?)\s*<sip\:(.+)>\s*$
$regex = '~^\s*(?:"([^"]+)"\s*)?<sip:(\d+)@.+>\s*~i';
$matches = array();
if( preg_match($regex, $string, $matches)){
    list(, $name, $phone) = $matches;
}