Fernand Royale Fernand Royale - 1 month ago 12
PHP Question

Preg_match_all split multiple occurrences

I have a string like this:

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



-Just one example is not just that.


I did this regular expression

preg_match_all("/[0-9][0-9](?:\|)(?:https\:\/\/)(.*?)/", string, string2);


But it only takes
number|https:


I wonder how do I get it and only stop when you find the next occurrence of the regular expression and separate it into different arrays

Answer

Try this:

$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);

Results array in $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