2011-08-08 10 views
0

私は2つの文字列の間に文字列を取得したいと思います。preg_match_all 2つの文字列の間のテキストを取得

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

基本的に私はurl()

間のすべてのテキストを取得したいと思いますが、誰かが私を助けることができる願っています。ありがとう!

+0

のためにこれを試してみてください。それから、私たちは助けようとします。 –

答えて

2
preg_match('~[(](.+?)[)]~',$string,$matches); 
+0

これには括弧が含まれています。 –

+1

@Peter:$ matches [1] – RiaD

+0

ああはい.......... –

0

この正規表現を試してみてください:

/url\s*\([^\)]+\)/ 
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

あなたが最初に自分を試してみて、ここにコードを投稿する必要があり、この特定の状況

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

空の文字列を返すので、例えば、あなたが使っている場合は良い解決法ではありません。 {START} {STOP}の文字列を何らかのタグとして使用します。 –