0

私はこのURLを持っています:../foo::bar{2}233{13}171{1}1{20}0.html

パラメータは{}、値の後ろにあります。

これにより、中括弧にもかかわらず、1 つのパラメーターを取得できます。

if (false !== strpos($url,'{2}')) {
        echo 'success';
    } else {
        echo 'error';
}

の背後にある各値を取得したい{}

4

3 に答える 3

1

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
*/

この方法はかなり冗長かもしれませんが、正規表現を使用してこれを行う方法がわかりません。これはまた、人々にとって理解しやすいようです。

于 2013-07-23T07:11:27.523 に答える
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);

ダブルエスケープを無視すると、次のようになります。

  • {を見つける
  • } までの 1 桁以上
  • 単語境界が現れるまで、1 桁以上
于 2013-07-23T07:16:43.980 に答える
0

これを試して。

$pieces = explode("}", $str);

すべての奇数インデックス要素を取得します。

$pieces[1],$pieces[3],$pieces[5]

等...

于 2013-07-23T07:21:44.380 に答える