我经常在正则表达式方面遇到麻烦,我基本上有一个URL,例如:
http://somedomain.com/something_here/bla/bla/bla/bla.jpg
我需要使用preg_replace()将something_here
替换为空字符串,并保留其他所有内容。
我尝试过以下方法,但它替换了错误的部分:
$image[0] = preg_replace('/http:\/\/(.*)\/(.*)\/wp-content\/uploads\/(.*)/','$2' . '',$image[0]);
这最终只留下了我想要替换的部分,而不是实际替换它!
parse_url()
函数,并在路径部分进行替换。 - Ja͢ck