ai didi

php - 用于从 SIP header 中提取 SIP 号码的正则表达式

转载 作者:行者123 更新时间:2023-12-04 04:48:26 24 4
gpt4 key购买 nike

来自 header 的可能 SIP:

"unknown-caller-name" <sip:unknown-ani@pbx.domain.com:5066;user=phone>
"Henry Tirta" <sip:951@domain.com>

我需要在 <sip: 之间提取 SIP 号码& @使用正则表达式从 php 中的上述标题。此 SIP 号码长度会有所不同。
$from = "\"User Name\" <sip:199@pbx.testdomain.com>";
$matches = array();
$pattern = '/[A-Za-z0-9_-]+@[A-Za-z0-9_-]+\.([A-Za-z0-9_-][A-Za-z0-9_]+)/';
preg_match($pattern,$from,$matches);
$number = explode('@', $matches[0])[0];
echo $number;

有没有更好的方法来做到这一点?

最佳答案

让我们简单点:

$from = '"unknown-caller-name" <sip:unknown-ani@pbx.domain.com:5066;user=phone>';
if(preg_match('#<sip:(.*?)@#', $from, $id)){
echo $id[1];
}else{
echo 'no match';
}

说明:
  • <sip: : 匹配 <sip:
  • (.*?) :匹配和分组所有不贪婪的东西,直到...
  • @ :@找到/匹配。
  • 关于php - 用于从 SIP header 中提取 SIP 号码的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17810678/

    24 4 0
    文章推荐: qt - QVector 有最大尺寸吗?
    文章推荐: SQL - 在记录存在的第一个日期返回记录
    文章推荐: python - 在单元测试中使 docopt 解析包含空格的参数
    文章推荐: R - 聚合因子/字符变量
    行者123
    个人简介

    我是一名优秀的程序员,十分优秀!

    滴滴打车优惠券免费领取
    滴滴打车优惠券
    全站热门文章
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com