我正在尝试找到一个正则表达式来转换css代码中的相对网址' url()'。到目前为止,这就是我所拥有的:
$domain = "http://example.com/";
$html = "url(1.css), url(' 1.css'), url( \"1.css\")";
$rep['/url(\s*)\((\s*)"(\s*)(?!https?:\/\/)(?!data:)(?!#)/i'] = 'url("'.$domain;
$rep["/url(\s*)\((\s*)'(\s*)(?!https?:\/\/)(?!data:)(?!#)/i"] = "url('".$domain;
$html = preg_replace(
array_keys($rep),
array_values($rep),
$html
);
echo $html;
当前输出:
url(1.css), url('http://example.com/1.css'), url("http://example.com/1.css")
期望输出:
url(http://example.com/1.css), url('http://example.com/1.css'), url("http://example.com/1.css")
答案 0 :(得分:1)
你期待这个吗?希望它能正常工作。
正则表达式: /url[\s]*\([\s]*(?!'|\")(?!https?:\/\/)(?!data:)(?!#)
此正则表达式将与url (
匹配,之后将不会有"
或'
。
<?php
ini_set('display_errors', 1);
$domain = "http://example.com/";
$html = "url(1.css), url(' 1.css'), url( \"1.css\")";
$rep['/url[\s]*\([\s]*"[\s]*(?!https?:\/\/)(?!data:)(?!#)/i'] = 'url("'.$domain;
$rep["/url[\s]*\([\s]*'[\s]*(?!https?:\/\/)(?!data:)(?!#)/i"] = "url('".$domain;
$rep["/url[\s]*\([\s]*(?!'|\")(?!https?:\/\/)(?!data:)(?!#)/i"] = "url(".$domain;
$html = preg_replace(
array_keys($rep),
array_values($rep),
$html
);
echo $html;