使用 str_replace 更改 url 的更好方法,而不是正则表达式

发布于 2024-09-30 20:57:44 字数 117 浏览 0 评论 0原文

我正在更改 css 文件网址,例如 str_replace('url(', 'url(somelocation/', $content); 现在我想排除绝对路径,例如 url(/ 有人建议吗?

I was changing css file urls like str_replace('url(', 'url(somelocation/', $content);
now I want to exclude, absolute path ones, like url(/ does any one suggest something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

任谁 2024-10-07 20:57:44
preg_replace('@url\(([^/].*)\)$@', preg_quote($location) . '$1', $content);
preg_replace('@url\(([^/].*)\)$@', preg_quote($location) . '$1', $content);
ˇ宁静的妩媚 2024-10-07 20:57:44
$location = 'somelocation'; // or however you're getting somelocation
if (strpos($location, '/') === 0) {
    $location = substr($location, 1);
}
str_replace('url(', 'url(' . $location, $content);
$location = 'somelocation'; // or however you're getting somelocation
if (strpos($location, '/') === 0) {
    $location = substr($location, 1);
}
str_replace('url(', 'url(' . $location, $content);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文