【问题标题】:better way for url change using str_replace, not regular expressions使用 str_replace 更改 url 的更好方法,而不是正则表达式
【发布时间】:2010-11-08 21:47:34
【问题描述】:
我正在更改 css 文件 URL,例如 str_replace('url(', 'url(somelocation/', $content);
现在我想排除绝对路径,比如 url(/ 有人建议什么吗?
【问题讨论】:
标签:
php
css
url
str-replace
【解决方案1】:
preg_replace('@url\(([^/].*)\)$@', preg_quote($location) . '$1', $content);
【解决方案2】:
$location = 'somelocation'; // or however you're getting somelocation
if (strpos($location, '/') === 0) {
$location = substr($location, 1);
}
str_replace('url(', 'url(' . $location, $content);