我想在2个字符串之间获取字符串。
background:url(images/cont-bottom.png) no-repeat;
基本上我想在url(
和)
答案 0 :(得分:2)
preg_match('~[(](.+?)[)]~',$string,$matches);
答案 1 :(得分:2)
<?
$css_file =
'background:url(images/cont-bottom.png) no-repeat;
background:url(images/cont-left.png) no-repeat;
background:url(images/cont-top.png) no-repeat;
background:url(images/cont-right.png) no-repeat;';
//matches all images inside the css file and loop the results
preg_match_all('/url\((.*?)\)/i', $css_file, $css_images, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($css_images[0]); $i++) {
echo $css_images[1][$i]."<br>";
}
/*
Outputs:
images/cont-bottom.png
images/cont-left.png
images/cont-top.png
images/cont-right.png
*/
?>
答案 2 :(得分:0)
试试这个正则表达式:
/url\s*\([^\)]+\)/
答案 3 :(得分:0)
(.*?)
不会继续换行以搜索匹配项,但是(.*)
将会继续换行
$string = 'background:url(images/cont-bottom.png) no-repeat;';
preg_match_all("#background:url\((.*?)\)#", $string, $match);
echo $match[1][0];
输出:
images/cont-bottom.png
答案 4 :(得分:-2)
针对这种特殊情况尝试这个
function getInbetweenStrings($start, $end, $str){
$matches = array();
$regex = "/$start(.*)$end/";
preg_match_all($regex, $str, $matches);
return $matches[1];
}
$str = "background:url(images/cont-bottom.png) no-repeat;";
$str_arr = getInbetweenStrings("\(", "\)", $str);
echo '<pre>';
print_r($str_arr);