私はこのURLを持っています:../foo::bar{2}233{13}171{1}1{20}0.html
パラメータは{}
、値の後ろにあります。
これにより、中括弧にもかかわらず、1 つのパラメーターを取得できます。
if (false !== strpos($url,'{2}')) {
echo 'success';
} else {
echo 'error';
}
の背後にある各値を取得したい{}
。
preg_match_all()を使用してみてください
$str = '../foo::bar{2}233{13}171{1}1{20}0.html';
$pattern = "/\{\d+\}/";
preg_match_all($pattern, $str, $matches);
$prevStart = 0;
foreach($matches[0] as $match)
{
$matchLen = strlen($match);
$matchPos = strpos($str, $match, $prevStart);
// Try to find the position of the next open curly brace
$openCurlyPos = strpos($str, '{', $matchPos + $matchLen);
// In case there is none found (.html comes up next), search for the . instead
if($openCurlyPos === false)
$openCurlyPos = strpos($str, '.', $matchPos + $matchLen);
$length = $openCurlyPos - ($matchPos + $matchLen);
$prevStart = $openCurlyPos;
echo $match.': '.substr($str, $matchPos + $matchLen, $length).'<br />';
}
/*
Result:
{2}: 233
{13}: 171
{1}: 1
{20}: 0
*/
この方法はかなり冗長かもしれませんが、正規表現を使用してこれを行う方法がわかりません。これはまた、人々にとって理解しやすいようです。
これを使用しpreg_match_all
てキーと値を抽出できます。これを行うためのサンプル パターンを次に示します。
$matches = null;
$returnValue = preg_match_all('/\\{(\\d+)\\}(\\d+)\\b/', '../foo::bar{2}233{13}171{1}1{20}0.html', $matches, PREG_SET_ORDER);
ダブルエスケープを無視すると、次のようになります。
これを試して。
$pieces = explode("}", $str);
すべての奇数インデックス要素を取得します。
$pieces[1],$pieces[3],$pieces[5]
等...