私は2つの文字列の間に文字列を取得したいと思います。preg_match_all 2つの文字列の間のテキストを取得
background:url(images/cont-bottom.png) no-repeat;
基本的に私はurl(
と)
間のすべてのテキストを取得したいと思いますが、誰かが私を助けることができる願っています。ありがとう!
私は2つの文字列の間に文字列を取得したいと思います。preg_match_all 2つの文字列の間のテキストを取得
background:url(images/cont-bottom.png) no-repeat;
基本的に私はurl(
と)
間のすべてのテキストを取得したいと思いますが、誰かが私を助けることができる願っています。ありがとう!
preg_match('~[(](.+?)[)]~',$string,$matches);
この正規表現を試してみてください:
/url\s*\([^\)]+\)/
<?
$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
*/
?>
あなたが最初に自分を試してみて、ここにコードを投稿する必要があり、この特定の状況
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);
空の文字列を返すので、例えば、あなたが使っている場合は良い解決法ではありません。 {START} {STOP}の文字列を何らかのタグとして使用します。 –
のためにこれを試してみてください。それから、私たちは助けようとします。 –