用preg_replace替换字符串

时间:2022-05-18 08:46:30

I have a URL like this:

我有这样的网址:

http://Example.com/mobile-ds-cams/mobile-gg-cams/ddd-webcams

Example:

例:

$pattern = '/http://Example.com/(\w+)/(\w+)/(\w+)/i';               
$replacement="http://Example.com/$2/$3";
$appUrl= preg_replace($pattern, $replacement, $appUrl);

What I want to achieve is this

我想要实现的是这个

http://Example.com/mobile-gg-cams/ddd-webcams

I am trying to keep 2 "sub-URLs" instead of 3. but it doesn't work..why?

我试图保留2个“子网址”而不是3.但它不起作用..为什么?

3 个解决方案

#1


0  

It doesn't work correctly because your expression contains characters with special meaning in a regex that have not been properly quoted.

它无法正常工作,因为您的表达式包含未正确引用的正则表达式中具有特殊含义的字符。

To be 100% certain, use preg_quote like this:

要100%确定,请使用preg_quote,如下所示:

$url = 'http://Example.com/'
$pattern = preg_quote($url.'{word}/{word}/{word}', '/');
$pattern = str_replace($pattern, '{word}', '(\w+)');
$pattern = "/$pattern/i";
$replacement = $url.'$2/$3';
$appUrl= preg_replace($pattern, $replacement, $appUrl);

Otherwise, it's simply too easy to get things wrong. For example, all of the other answers currently posted here get it wrong because they do not properly escape the . in Example.com. You can test this yourself if you feed them a string like e.g. http://Example!com, where ! can be any character you like.

否则,这很容易弄错。例如,目前发布的所有其他答案都是错误的,因为它们没有正确地逃脱。在Example.com中。你可以自己测试一下,如果你给它们喂一个像http://示例!com,在哪里!可以是你喜欢的任何角色。

Additionally, you are using strings such as $2 inside a double-quoted string literal, which is not a good idea in PHP because IMHO it's easy to get carried away. Better make that singly quoted and be safe.

另外,你在双引号字符串文字中使用$ 2等字符串,这在PHP中不是一个好主意,因为恕我直言,它很容易被带走。更好地使它单独引用并且安全。

#2


3  

You need to escape your forward-slashes within the pattern, or use different pattern delimiters.

您需要在模式中转义正斜杠,或使用不同的模式分隔符。

$pattern = '/http:\/\/Example\.com\/(\w+)\/(\w+)\/(\w+)/i';               

$pattern = '#http://Example\.com/(\w+)/(\w+)/(\w+)#i';               

#3


0  

Escape the slashes like this:

像这样逃避斜杠:

$pattern = '/http:\/\/Example.com\/(\w+)\/(\w+)\/(\w+)/i';               
$replacement="http://Example.com/$2/$3";
$appUrl= preg_replace($pattern, $replacement, $appUrl);

#1


0  

It doesn't work correctly because your expression contains characters with special meaning in a regex that have not been properly quoted.

它无法正常工作,因为您的表达式包含未正确引用的正则表达式中具有特殊含义的字符。

To be 100% certain, use preg_quote like this:

要100%确定,请使用preg_quote,如下所示:

$url = 'http://Example.com/'
$pattern = preg_quote($url.'{word}/{word}/{word}', '/');
$pattern = str_replace($pattern, '{word}', '(\w+)');
$pattern = "/$pattern/i";
$replacement = $url.'$2/$3';
$appUrl= preg_replace($pattern, $replacement, $appUrl);

Otherwise, it's simply too easy to get things wrong. For example, all of the other answers currently posted here get it wrong because they do not properly escape the . in Example.com. You can test this yourself if you feed them a string like e.g. http://Example!com, where ! can be any character you like.

否则,这很容易弄错。例如,目前发布的所有其他答案都是错误的,因为它们没有正确地逃脱。在Example.com中。你可以自己测试一下,如果你给它们喂一个像http://示例!com,在哪里!可以是你喜欢的任何角色。

Additionally, you are using strings such as $2 inside a double-quoted string literal, which is not a good idea in PHP because IMHO it's easy to get carried away. Better make that singly quoted and be safe.

另外,你在双引号字符串文字中使用$ 2等字符串,这在PHP中不是一个好主意,因为恕我直言,它很容易被带走。更好地使它单独引用并且安全。

#2


3  

You need to escape your forward-slashes within the pattern, or use different pattern delimiters.

您需要在模式中转义正斜杠,或使用不同的模式分隔符。

$pattern = '/http:\/\/Example\.com\/(\w+)\/(\w+)\/(\w+)/i';               

$pattern = '#http://Example\.com/(\w+)/(\w+)/(\w+)#i';               

#3


0  

Escape the slashes like this:

像这样逃避斜杠:

$pattern = '/http:\/\/Example.com\/(\w+)\/(\w+)\/(\w+)/i';               
$replacement="http://Example.com/$2/$3";
$appUrl= preg_replace($pattern, $replacement, $appUrl);