【问题标题】: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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-05
      • 1970-01-01
      • 1970-01-01
      • 2020-08-25
      • 2015-05-23
      • 1970-01-01
      相关资源
      最近更新 更多