• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php preg_mahch字符截取解决思路

php 搞代码 3年前 (2022-01-26) 22次浏览 已收录 0个评论

php preg_mahch字符截取

PHP code

<!---ecms Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->$tt = "Event: Dial  Privilege: call,all  SubEvent: Begin  Channel: [email protected];2  Destination: SIP/601-00001bca  CallerIDNum: 07016317212  CallerIDName: rc07016317212  ConnectedLineNum: 0267  ConnectedLineName: 0267  UniqueID: 1339380988.50590  DestUniqueID: 1339380988.50591  Dialstring: 601";preg_match("/(CallerIDNum:\s+\d+)\s+.+(UniqueID:\s\d+\.\d+)/",$tt,$out, PREG_OFFSET_CAPTURE,3);

希望的结果是 
[‘CallerIDNum’] =〉”07016317212″ ;
[‘CallerIDName’] =>”rc07016317212″ ;
[‘ConnectedLineNum’]=>”0267″;
[‘ConnectedLineName’]=>”0267″;
[‘UniqueID’]=>”1339380988.50590″;
[DestUniqueID]=>”1339380988.50591″;
。。。。。。。。。
类型这样,从那个字符串拆分一个名称对应一个值

——解决方案——————–

PHP code

$tt = "Event: Dial  Privilege: call,all  SubEvent: Begin  Channel: [email protected];2  Destination: SIP/601-00001bca  CallerIDNum: 07016317212  CallerIDName: rc07016317212  ConnectedLineNum: 0267  ConnectedLineName: 0267  UniqueID: 1339380988.50590  DestUniqueID: 1339380988.50591  Dialstring: 601";preg_replace('/(\w+):\s+([^\s]+)/e','$ar[$1]="$2"',$tt);//quit();print_r($ar);
------解决方案--------------------

PHP code

[User:root Time:13:15:35 Path:/home/liangdong/php]$ cat preg.php <?php$str = "Event: Dial  Privilege: call,all  SubEvent: ~来1源gaodai#ma#com搞*代#码1网搞代gaodaima码Begin  Channel: [email protected];2  Destination: SIP/601-00001bca  CallerIDNum: 07016317212  CallerIDName: rc07016317212  ConnectedLineNum: 0267  ConnectedLineName: 0267  UniqueID: 1339380988.50590  DestUniqueID: 1339380988.50591  Dialstring: 601";$nmatches = preg_match_all('/(CallerIDNum|CallerIDName|ConnectedLineNum|ConnectedLineName|UniqueID|DestUniqueID|Dialstring): (\S+)/', $str, $matches);print_r($matches);?>[User:root Time:13:15:36 Path:/home/liangdong/php]$ php preg.php Array(    [0] => Array        (            [0] => CallerIDNum: 07016317212            [1] => CallerIDName: rc07016317212            [2] => ConnectedLineNum: 0267            [3] => ConnectedLineName: 0267            [4] => UniqueID: 1339380988.50590            [5] => DestUniqueID: 1339380988.50591            [6] => Dialstring: 601        )    [1] => Array        (            [0] => CallerIDNum            [1] => CallerIDName            [2] => ConnectedLineNum            [3] => ConnectedLineName            [4] => UniqueID            [5] => DestUniqueID            [6] => Dialstring        )    [2] => Array        (            [0] => 07016317212            [1] => rc07016317212            [2] => 0267            [3] => 0267            [4] => 1339380988.50590            [5] => 1339380988.50591            [6] => 601        ))
------解决方案--------------------
把我的\S+改成\S*即可。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php preg_mahch字符截取解决思路
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址