i want to raplace dots in url with preg_replace
我想用preg_replace在url中放置点
How can i do it?
我该怎么做?
The URL is:
URL是:
HTTP://localhost/../images/
I need it to become:
我需要它成为:
HTTP://本地主机/图像/
I try to make it like this:
我试着这样做:
$url = 'http://localhost/../images/';
$final = preg_replace('\/', '/\..\/', $url);
I try also like this:
我也尝试这样:
$url = 'http://localhost/../images/';
$final = preg_replace('/', '/../', $url);
1 个解决方案
#1
1
Your preg_replace
usage is incorrect, but you don't need a regex for this anyway. For static replacements just use str_replace
.
您的preg_replace使用不正确,但无论如何您都不需要正则表达式。对于静态替换,只需使用str_replace。
$url = 'http://localhost/../images/';
$url = str_replace('..', '', $url);
but your probably also should include the /
in the search.
但你可能也应该在搜索中包含/。
Your preg_replace
is inverted the pattern is the first parameter and the replacement value second. http://php.net/manual/en/function.preg-replace.php
你的preg_replace被反转,pattern是第一个参数,第二个是替换值。 http://php.net/manual/en/function.preg-replace.php
So the correct preg_replace
would be:
所以正确的preg_replace将是:
$url = 'http://localhost/../images/';
$final = preg_replace('/\.\./', '/', $url);
Also this is putting a third /
between the domain and directory. The /
s are delimiters in the pattern, did you mean for that?
这也是域和目录之间的第三个/。 / s是模式中的分隔符,你的意思是什么?
Note the .
s are special characters and need to be escaped.
请注意.s是特殊字符,需要进行转义。
#1
1
Your preg_replace
usage is incorrect, but you don't need a regex for this anyway. For static replacements just use str_replace
.
您的preg_replace使用不正确,但无论如何您都不需要正则表达式。对于静态替换,只需使用str_replace。
$url = 'http://localhost/../images/';
$url = str_replace('..', '', $url);
but your probably also should include the /
in the search.
但你可能也应该在搜索中包含/。
Your preg_replace
is inverted the pattern is the first parameter and the replacement value second. http://php.net/manual/en/function.preg-replace.php
你的preg_replace被反转,pattern是第一个参数,第二个是替换值。 http://php.net/manual/en/function.preg-replace.php
So the correct preg_replace
would be:
所以正确的preg_replace将是:
$url = 'http://localhost/../images/';
$final = preg_replace('/\.\./', '/', $url);
Also this is putting a third /
between the domain and directory. The /
s are delimiters in the pattern, did you mean for that?
这也是域和目录之间的第三个/。 / s是模式中的分隔符,你的意思是什么?
Note the .
s are special characters and need to be escaped.
请注意.s是特殊字符,需要进行转义。