<?php
$list = "http://www.example.com/example1/
http://www.example.com/example2/
http://www.example.com/example3/
http://www.example.com/example4/
http://www.example.com/example.js
http://www.example.com/example.css
http://www.example.com/example1.js?v=123
http://www.example.com/{path}
http://www.example.com/feed/
http://www.example.com/?p=66";
$arr = preg_split("/[\r\n]+/",$list);
// check our input array
print_r($arr);
$map = array();
foreach($arr as $v){
if(!preg_match("/({path}|\.(js|css)|\?p=\d+|\/feed\/)$/",$v)){
$map[] = $v;
}
};
// check our output array
print_r($map);
?>
这假设您要匹配不以{path}
or.css
或.js
or ?p=##
(其中 # 是数字)或结尾的网址/feed/
。这就是为什么还有/example1.js?v=123
被匹配的原因。要使其匹配字符串中的任何位置,而不是仅在末尾,请$
从正则表达式的末尾(就在 word 之后feed
)删除 。
我的控制台输出:
Array
(
[0] => http://www.example.com/example1/
[1] => http://www.example.com/example2/
[2] => http://www.example.com/example3/
[3] => http://www.example.com/example4/
[4] => http://www.example.com/example.js
[5] => http://www.example.com/example.css
[6] => http://www.example.com/example1.js?v=123
[7] => http://www.example.com/{path}
[8] => http://www.example.com/feed/
[9] => http://www.example.com/?p=66
)
Array
(
[0] => http://www.example.com/example1/
[1] => http://www.example.com/example2/
[2] => http://www.example.com/example3/
[3] => http://www.example.com/example4/
[4] => http://www.example.com/example1.js?v=123
)