欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php preg_mahch字符截取解决思路

程序员文章站 2022-05-31 20:41:16
...
php preg_mahch字符截取
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_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 [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*即可。
php preg_mahch字符截取解决思路

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频