Preg_split模式用于拆分URL,如parseURL

时间:2016-09-25 07:40:30

标签: php regex

我需要将URL拆分为数组。

示例:

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

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

preg_split

1 个答案:

答案 0 :(得分: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 
)