I always have trouble with regex, I basically have a url, for example:
我一直有正则表达式的麻烦,我基本上有一个网址,例如:
http://somedomain.com/something_here/bla/bla/bla/bla.jpg
What I need is a preg_replace() to replace the something_here
with an empty string, and leave everything else in tact.
我需要的是preg_replace()用一个空字符串替换something_here,并保留其他所有内容。
I have tried the following and it replaces the wrong parts:
我尝试了以下内容,它取代了错误的部分:
$image[0] = preg_replace('/http:\/\/(.*)\/(.*)\/wp-content\/uploads\/(.*)/','$2' . '',$image[0]);
This ends up leaving only the part I want to replace, rather than actually replacing it!
这最终只留下我要替换的部分,而不是实际替换它!
3 个解决方案
#1
6
The following code is based on the description you provided:
以下代码基于您提供的说明:
$url = 'http://somedomain.com/something_here/bla/bla/bla/bla.jpg';
$output = preg_replace('#^(https?://[^/]+/)[^/]+/(.*)$#', '$1$2', $url);
echo $output; // http://somedomain.com/bla/bla/bla/bla.jpg
Explanation:
-
^
: match begin of line -
(
: start matching group 1-
https?://
: match http or https protocol -
[^/]+
: match anything except/
one or more times -
/
: match/
https?://:匹配http或https协议
[^ /] +:匹配除/一次或多次以外的任何内容
/ : 比赛 /
-
-
)
: end matching group 1 -
[^/]+
: match anything except/
one or more times -/
: match/
-
(
: start matching group 2-
.*
: match anything zero or more times (greedy)
。*:匹配任何零次或多次(贪婪)
-
-
)
: end matching group 2 -
$
: match end of line
^:匹配行的开头
(:开始匹配组1 https?://:匹配http或https协议[^ /] +:除了/一次或多次匹配任何东西/:匹配/
):结束匹配组1
[^ /] +:除了/一次或多次匹配任何东西 - /:匹配/
(:开始匹配组2。*:匹配任何零次或多次(贪婪)
):结束匹配组2
$:匹配行尾
#2
2
You could do this:
你可以这样做:
$image[0] = preg_replace('!^(http://[^/]*)/[^/]*!', '$1', $image[0]);
Or you might consider just splitting the string to work on its individual components:
或者您可以考虑将字符串拆分为适用于其各个组件:
$parts = explode('/', $image[0]);
unset($parts[3]);
$image[0] = implode('/', $parts);
#3
1
You could do this with a simple string replace:
您可以使用简单的字符串替换来执行此操作:
$image[0] = str_replace('/wp-content/uploads/', '/', $image[0]);
Or if you want to use a regular expression:
或者,如果要使用正则表达式:
$image[0] = preg_replace('~(http://.*?)/wp-content/uploads/(.*)~', '$1/$2', $image[0]);
#1
6
The following code is based on the description you provided:
以下代码基于您提供的说明:
$url = 'http://somedomain.com/something_here/bla/bla/bla/bla.jpg';
$output = preg_replace('#^(https?://[^/]+/)[^/]+/(.*)$#', '$1$2', $url);
echo $output; // http://somedomain.com/bla/bla/bla/bla.jpg
Explanation:
-
^
: match begin of line -
(
: start matching group 1-
https?://
: match http or https protocol -
[^/]+
: match anything except/
one or more times -
/
: match/
https?://:匹配http或https协议
[^ /] +:匹配除/一次或多次以外的任何内容
/ : 比赛 /
-
-
)
: end matching group 1 -
[^/]+
: match anything except/
one or more times -/
: match/
-
(
: start matching group 2-
.*
: match anything zero or more times (greedy)
。*:匹配任何零次或多次(贪婪)
-
-
)
: end matching group 2 -
$
: match end of line
^:匹配行的开头
(:开始匹配组1 https?://:匹配http或https协议[^ /] +:除了/一次或多次匹配任何东西/:匹配/
):结束匹配组1
[^ /] +:除了/一次或多次匹配任何东西 - /:匹配/
(:开始匹配组2。*:匹配任何零次或多次(贪婪)
):结束匹配组2
$:匹配行尾
#2
2
You could do this:
你可以这样做:
$image[0] = preg_replace('!^(http://[^/]*)/[^/]*!', '$1', $image[0]);
Or you might consider just splitting the string to work on its individual components:
或者您可以考虑将字符串拆分为适用于其各个组件:
$parts = explode('/', $image[0]);
unset($parts[3]);
$image[0] = implode('/', $parts);
#3
1
You could do this with a simple string replace:
您可以使用简单的字符串替换来执行此操作:
$image[0] = str_replace('/wp-content/uploads/', '/', $image[0]);
Or if you want to use a regular expression:
或者,如果要使用正则表达式:
$image[0] = preg_replace('~(http://.*?)/wp-content/uploads/(.*)~', '$1/$2', $image[0]);