如何移除多个斜线在URI与“PREG”或“htaccess的”如何删除多个斜线在URI与“PREG”或“htaccess的”
site.com/edition/new/// - >位点.COM /编辑/新/
site.com/edition///new/ - > site.com/edition/new/
感谢
如何移除多个斜线在URI与“PREG”或“htaccess的”如何删除多个斜线在URI与“PREG”或“htaccess的”
site.com/edition/new/// - >位点.COM /编辑/新/
site.com/edition///new/ - > site.com/edition/new/
感谢
在正则表达式中使用加号+
意味着一个或多个前一个字符的出现。因此,我们可以在preg_replace函数添加它的只是其中之一来替换一个或多个/
发生
$url = "site.com/edition/new///";
$newUrl = preg_replace('/(\/+)/','/',$url);
// now it should be replace with the correct single forward slash
echo $newUrl
编辑:哈我读了这个问题作为“没有preg”哦:3
function removeabunchofslashes($url){
$explode = explode('://',$url);
while(strpos($explode[1],'//'))
$explode[1] = str_replace('//','/',$explode[1]);
return implode('://',$explode);
}
echo removeabunchofslashes('http://www.site.com/edition////new///');
http://domain.com/test/test/>http://domain.com/test/test
# Strip trailing slash(es) from uri
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)[/]+$ $1 [NC,R,L]
http://domain.com//test//test//>http://domain.com/test/test/
# Merge multiple slashes in uri
RewriteCond %{THE_REQUEST} ^[A-Z]+\ //*(.+)//+(.*)\ HTTP
RewriteRule^/%1/%2 [R,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ //+(.*)\ HTTP
RewriteRule^/%1 [R,L]
变化R到R = 301,如果一切正常后测试...
有谁知道如何使用上述方法在查询中保留双斜杠?
(例如:?/测试//测试//测试=测试//测试> /测试/检验/测试=测试//测试)
$url = 'http://www.abc.com/def/git//ss';
$url = preg_replace('/([^:])(\/{2,})/', '$1/', $url);
// output http://www.abc.com/def/git/ss
$url = 'https://www.abc.com/def/git//ss';
$url = preg_replace('/([^:])(\/{2,})/', '$1/', $url);
// output https://www.abc.com/def/git/ss
好主意 但如何做检查“版本” 后,如本例 $ url =“site.com/edition///new///”; $ newUrl = preg_replace('/ edition(\/+)/','/',$ url); 如果url是'http://test.com/edition ///// new',我不知道如何应用 – Lelis 2011-06-14 05:51:34
它不起作用! – 2017-08-04 11:22:57