preg_match_all получить текст между 2 строками - PullRequest
0 голосов
/ 08 августа 2011

Я хотел бы получить строку между 2 строками.

background:url(images/cont-bottom.png) no-repeat;

В основном я хотел бы получить весь текст от url( до )

Надеюсь, кто-нибудь может мне помочь. спасибо!

Ответы [ 5 ]

2 голосов
/ 17 августа 2011
<?
$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 голосов
/ 08 августа 2011
preg_match('~[(](.+?)[)]~',$string,$matches);
0 голосов
/ 15 июля 2018

(.*?) не будет переходить на новую строку для поиска совпадений, но (.*) будет переходить на новую строку

$string = 'background:url(images/cont-bottom.png) no-repeat;';

preg_match_all("#background:url\((.*?)\)#", $string, $match);

echo  $match[1][0];

Выход:

images/cont-bottom.png
0 голосов
/ 27 февраля 2013

попробуйте это для этой конкретной ситуации

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);
0 голосов
/ 08 августа 2011

Попробуйте это регулярное выражение:

/url\s*\([^\)]+\)/
...