Php 如何在不必编写多个str replace语句的情况下清理大量URL?
以下是几个需要清理的URL示例:Php 如何在不必编写多个str replace语句的情况下清理大量URL?,php,regex,url,preg-replace,Php,Regex,Url,Preg Replace,以下是几个需要清理的URL示例: 应成为: 应成为: 应成为: 应成为: 应成为: 无需多次写入str_replace(),是否有方法清理url?此解决方案对您有好处: <?php // I merged your examples in this URL $url= "http://example.com//path/?¶m=value"; // Separate URL $parsed = parse_url($url); // Path cleanup $
- 应成为:
- 应成为:
- 应成为:
- 应成为:
- 应成为:
无需多次写入
str_replace()
,是否有方法清理url?此解决方案对您有好处:
<?php
// I merged your examples in this URL
$url= "http://example.com//path/?¶m=value";
// Separate URL
$parsed = parse_url($url);
// Path cleanup
$path = trim($parsed['path'],'/');
// Query string cleanup
$query = trim($parsed['query'],'&');
// Concatenate the URL parts
echo sprintf("%s://%s/%s?%s",$parsed['scheme'],$parsed['host'],$path,$query);
Sahil的方法非常复杂,包括6个替换元素、模式中不必要的字符转义以及不必要的重复字符上的有限量词。事实上,这个简单的url无法更正:
http://example.com//path1
在您的项目中实现这个更短、更快、更干净、更可读的方法:
代码():
模式说明:
/(?匹配2个或更多不带冒号的斜杠;替换为单斜杠
/\?&/
匹配一个问号,后跟一个符号和;替换为问号
/[?&]$/
如果是问号或符号,则匹配最后一个字符;删除
另外,下面是我对url解析方法的看法:()
代码:
输出:
array (
0 => 'http://example.com/path/',
1 => 'http://example.com/path/',
2 => 'http://example.com/path/?param=one',
3 => 'http://example.com/',
4 => 'http://example.com/path/subpath/?param=one',
)
array (
0 => 'http://example.com/path/to/dir/4/ok',
1 => 'http://example.com/path/',
2 => 'http://example.com/path/?param=one',
3 => 'http://www.example.com/',
4 => 'http://example.com/path/subpath/?param=one',
)
url组件处理说明:
path
元素上的preg\u replace()
模式将匹配一个或多个斜杠,并用一个斜杠替换。这也可以通过使用~/+(?=/)~
或~(?请提交您尝试过但尚未尝试过的内容,我会尽快提交。我喜欢您的方法,但如果http://example.com//path1//path2/////?¶m=value&
使用URL解析器绝对是一种优越的方法。这里的实现并不完美,但它是健壮实现的良好开端。+1
$urls=array(
"http://example.com//path//to///dir////4/ok",
"http://example.com/path/?&&",
"http://example.com/path/?¶m=one",
"http://www.example.com///?&",
"http://example.com/path/subpath///?param=one&");
foreach($urls as $url){
$a=parse_url($url);
$clean_urls[]="{$a["scheme"]}://{$a["host"]}". // no problems expected from these elements
preg_replace('~/+~','/',$a["path"]). // reduce multiple consecutive slashes to single slash
(isset($a["query"]) && trim($a["query"],'&')!=''?'?'.trim($a["query"],'&'):''); // handle querystring
}
var_export($clean_urls);
array (
0 => 'http://example.com/path/to/dir/4/ok',
1 => 'http://example.com/path/',
2 => 'http://example.com/path/?param=one',
3 => 'http://www.example.com/',
4 => 'http://example.com/path/subpath/?param=one',
)