用破折号替换 URL 中的点

Replace dots in URL with dash

我想用 preg_replace

替换 url 中的点

我该怎么做?

URL是:

http://localhost/../images/

我需要它变成:

http://localhost/images/

我试着做成这样:

$url = 'http://localhost/../images/';
$final = preg_replace('\/', '/\..\/', $url);

我也这样尝试:

$url = 'http://localhost/../images/';
$final = preg_replace('/', '/../', $url);

您的 preg_replace 用法不正确,但无论如何您都不需要正则表达式。对于静态替换,只需使用 str_replace.

$url = 'http://localhost/../images/';
$url = str_replace('..', '', $url);

但您可能还应该在搜索中包含 /

你的preg_replace被颠倒了模式是第一个参数和第二个替换值。 http://php.net/manual/en/function.preg-replace.php

所以正确的 preg_replace 应该是:

$url = 'http://localhost/../images/';
$final = preg_replace('/\.\./', '/', $url);

这也是在域和目录之间放置第三个 // 是模式中的 delimiters,您是这个意思吗?

注意 . 是特殊字符,需要转义。