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

标签 php regex sip

来自 header 的可能 SIP:

"unknown-caller-name" <sip:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ee9b808580819980c38f8087ae9e8c96c08a81838f8780c08d8183" rel="noreferrer noopener nofollow">[email protected]</a>:5066;user=phone>
"Henry Tirta" <sip:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4c75797d0c2823212d2522622f2321" rel="noreferrer noopener nofollow">[email protected]</a>>

我需要提取<sip:之间的SIP号码& @使用正则表达式从 php 中的上述 header 中获取。此 SIP 号码长度会有所不同。

$from = "\"User Name\" <sip:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d0e1e9e990a0b2a8fea4b5a3a4b4bfbdb1b9befeb3bfbd" rel="noreferrer noopener nofollow">[email protected]</a>>";
$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:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99ecf7f2f7f6eef7b4f8f7f0d9e9fbe1b7fdf6f4f8f0f7b7faf6f4" rel="noreferrer noopener nofollow">[email protected]</a>: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/

相关文章:

php - 将特定部分 div 转换为 pdf

java - 使用 Java 替换来自阿拉伯语推文的表情符号 Unicode 范围

regex - 如何使用正则表达式在Powershell中删除字符串的一部分

android - Asterisk :Don't allow to call if SIP peer is not registered

java - sip软电话对等点源代码运行

php - cakephp中的数据解码问题

php - headless (headless) Chromium 浏览器始终显示验证码

php - CodeIgniter 3 查询 ESCAPE '!' 忽略 WHERE 语句?

regex - 如何使用正则表达式匹配 VBA 中的简单数字模式

android - 如何在 Android 上使用 pjsip 发送 dtmf?