2016-09-25 53 views
1

我需要將一個URL拆分成一個數組。Preg_split模式來拆分URL像parseURL

實施例:

https://demoURL:44355/test/new/demo.html/#ContactPerson?language=EN 

array(
    'http:/', 
    'demoURL:44355', 
    'test/new/demo.html/#ContactPerson?language=EN' 
) 

preg_split

+0

你嘗試過什麼? http://stackoverflow.com/questions/11480763/how-to-get-parameters-from-this-url-string – MarZab

+1

爲什麼不使用'parse_url'函數? http://php.net/manual/en/function.parse-url.php – GeorgeQ

+0

改進格式化一點點 – gofr1

回答

0

試試:

<?php 
$body = "https://demoURL:44355/test/new/demo.html/#ContactPerson?language=EN"; 
$regex = '/((?<=\/)(?=\/))|((?<=\/{2})(?=[A-Za-z]+:[0-9]+))|(?<=[0-9]{1})(?=\/[a-z\/.?=#]+)/'; 
$url = preg_split($regex, $body); 
print_r($url); 
?> 

您將獲得:

Array ( 
    [0] => https:/ 
    [1] =>/
    [2] => demoURL:44355 
    [3] => /test/new/demo.html/#ContactPerson?language=EN 
)