Is there a way to find all urls inside a string and save each trunk of the original message in an array?
My goal is to intercepts url, change it with a function that change some parameters in the url, and rebuild original string.
Example:
$original_string = "Hi, this is a list of urls: http://www.google.it, www.amazon.it, https://www.amzn.to/XXXXX and at the end we have www.example.it";
Expected result:
$result = array(
0 => "Hi, this is a list of urls: ",
1 => "http://www.google.it",
2 => ", ",
3 => "www.amazon.it",
4 => ", ",
5 => "https://www.amzn.to/XXXXX",
6 => " and at the end we have ",
7 => "www.example.it"
);
After this result, i can edit my link with a function i've already done and rebuild the string.
I can find all urls inside a string with: preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $original_string, $urls);
but i lost all other text...
UPDATE: tried this code as suggested, but i get strange result:
$x = preg_split('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $original_string, -1, PREG_SPLIT_DELIM_CAPTURE);
var_dump($x);
array(9) {
[0]=>
string(28) "Hi, this is a list of urls: "
[1]=>
string(1) "t"
[2]=>
string(2) ", "
[3]=>
string(1) "t"
[4]=>
string(2) ", "
[5]=>
string(1) "X"
[6]=>
string(24) " and at the end we have "
[7]=>
string(1) "t"
[8]=>
string(0) ""
}