开发者

How to split a PHP string into an array in which each element is either a text or a URL?

I need to split a PHP array into an array containing text and URLs. For instance, assuming

$string = "Hello, my name is http://www.audio.com/1234.mp3/. Today is https://radio.org/weather.wav";

The expected output should be something like:

$a[0] = "Hello, my name is";
$a[1] 开发者_如何学运维= "http://www.audio.com/1234.mp3/";
$a[2] = ". Today is";
$a[3] = "https://radio.org/weather.wav";

Any suggestions?

Thanks in advance,

Leo


You cannot split it easily. But a workaround would be to match it in pairs using something like:

preg_match_all('#(.*?)(https?://\S+(?<![,.]))\K#s', $str, $m,
               PREG_SET_ORDER);
$list = call_user_func_array("array_merge", $m);

The call_user_func_array is another workaround to avoid flatteing the array manually. This method will lead to empty entries in between however:

Array
(
    [0] => 
    [1] => Hello, my name is 
    [2] => http://www.audio.com/1234.mp3/
    [3] => 
    [4] =>  Today is 
    [5] => https://radio.org/weather.wav
)

Also note that the simplistic URL regex ate up the period. (Use exact character groups instead of lookbehind.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜