Rumah > pembangunan bahagian belakang > tutorial php > php preg_mahch字符截取解决思路

php preg_mahch字符截取解决思路

PHP中文网
Lepaskan: 2023-02-28 14:36:01
asal
1395 orang telah melayarinya

  php preg_mahch字符截取

PHP代码

$tt = "Event: Dial  Privilege: call,all  SubEvent: Begin  
Channel: Local/0267@from-queue-2962;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);
Salin selepas log masuk

希望的结果是 

['CallerIDNum'] =〉"07016317212" ;
['CallerIDName'] =>"rc07016317212" ;
['ConnectedLineNum']=>"0267";
['ConnectedLineName']=>"0267";
['UniqueID']=>"1339380988.50590";
[DestUniqueID]=>"1339380988.50591";
。。。。。。。。。
Salin selepas log masuk

类型这样,从那个字符串拆分一个名称对应一个值

解决方法:

方法一

PHP代码

$tt = "Event: Dial  Privilege: call,all  
SubEvent: Begin  Channel: Local/0267@from-queue-2962;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);
Salin selepas log masuk

方法二

PHP代码

[User:root Time:13:15:35 Path:/home/liangdong/php]$ cat preg.php 
<?php
$str = "Event: Dial  Privilege: call,all  SubEvent: Begin  
Channel: Local/0267@from-queue-2962;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(&#39;/(CallerIDNum|CallerIDName|ConnectedLineNum|ConnectedLineName|
UniqueID|DestUniqueID|Dialstring): (S+)/&#39;, $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
        )

)
Salin selepas log masuk

方法三

把我的S+改成S*即可。

 以上就是 php preg_mahch字符截取解决思路 的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan