Preg_match_all拆分多次出现

时间:2016-10-21 02:26:13

标签: php regex preg-match-all

我有一个这样的字符串:

string="59|https://site59.com20|https://site20.com30|https://site30.com16|https://site15.com66|https://site66.com29|https://site29.com";

     - 只有一个例子不仅仅是那个。

我做了这个正则表达
preg_match_all("/[0-9][0-9](?:\|)(?:https\:\/\/)(.*?)/", string, string2);

但只需要number|https:

我想知道如何获取它并且只在找到下一个正则表达式并将其分成不同的数组时停止

2 个答案:

答案 0 :(得分:1)

试试这个:

$string="59|https://site59.com20|https://site20.com30|https://site30.com16|https://site15.com66|https://site66.com29|https://site29.com";
preg_match_all("/(?:[0-9][0-9](?:\|)(?:https\:\/\/)(.*?)(?=[\d][\d]\||$))|([\d][\d]\|.*)/", $string, $matches);

$ matches中的结果数组:

[0] => 59|https://site59.com
[1] => 20|https://site20.com
[2] => 30|https://site30.com
[3] => 16|https://site15.com
[4] => 66|https://site66.com
[5] => 29|https://site29.com

答案 1 :(得分:0)

尝试使用preg_split

<?php

$string="59|https://site59.com20|https://site20.com30|https://site30.com16|https://site15.com66|https://site66.com29|https://site29.com";

$sites = preg_split("/[0-9][0-9](?:\|)(?:https\:\/\/)(.*?)/", $string);

foreach($sites as $site){
    echo "https://$site\n";
}
https://site59.com
https://site20.com
https://site30.com
https://site15.com
https://site66.com
https://site29.com