2

もう一度、正規表現で立ち往生しています。より高度な使用法を学ぶための良い資料はどこにもありません。

[image width="740" height="249" parameters=""]51lca7dn56.jpg[/image] に一致さ せようとしてい$cache->image_tag("$4", $1, $2, "$3")ます。

すべての[image]パラメータが存在する場合はすべてうまく機能しますが、何かが欠落している場合でも、一致させる必要があります。たとえば、[image width="740"]51lca7dn56.jpg[/image]

現在のコードは次のとおりです。

$text = preg_replace('#\[image width=\"(.*?)\" height=\"(.*?)\" parameters=\"(.*?)\"\](.*?)\[/image\]#e', '$cache->image_tag("$4", $1, $2, "$3")', $text);

いつも行き詰まっているのは正規表現だけなので、誰かが良いリソースを紹介してくれれば、この種の問題を自分で管理できれば幸いです。

私がやろうとしている私のダミーバージョンはこれです:

// match only [image]
$text = preg_replace('#\[image\](.*?)\[/image\]#si', '$cache->image_tag("$1", 0, 0, "")', $text);
// match only width
$text = preg_replace('#\[image width=\"(.*?)\"\](.*?)\[/image\]#si', '$cache->image_tag("$2", $1, 0, "")', $text);
// match only width and height
$text = preg_replace('#\[image width=\"(.*?)\" height=\"(.*?)\"\](.*?)\[/image\]#si', '$cache->image_tag("$3", $1, $2, "")', $text);
// match only all
$text = preg_replace('#\[image width=\"(.*?)\" height=\"(.*?)\" parameters=\"(.*?)\"\](.*?)\[/image\]#si', '$cache->image_tag("$4", $1, $2, $3)', $text);

(このコードは実際には期待どおりに機能しませんが、私のポイントをよりよく理解できます。)基本的にこの恐ろしい混乱をすべて1つのRE呼び出しにまとめたいと思います。

Ωmegaの答えに基づいてテストされ、機能する最終的なコード:

// Match: [image width="740" height="249" parameters="bw"]51lca7dn56.jpg[/image]
$text = preg_replace('#\[image\b(?=(?:[^\]]*\bwidth="(\d+)"|))(?=(?:[^\]]*\bheight="(\d+)"|))(?=(?:[^\]]*\bparameters="([^"]+)"|))[^\]]*\]([^\[]*)\[\/image\]#si', '$cache->image_tag("$4", $1, $2, "$3")', $text); // the end is #si, so it would be eaiser to debug, in reality its #e

ただし、幅または高さがない場合は、NULLではなく空が返されます。だから私はドローのアイデアを採用しましたpreg_replace_callback()

$text = preg_replace_callback('#\[image\b(?=(?:[^\]]*\bwidth="(\d+)"|))(?=(?:[^\]]*\bheight="(\d+)"|))(?=(?:[^\]]*\bparameters="([^"]+)"|))[^\]]*\]([^\[]*)\[\/image\]#', create_function(
'$matches',
'global $cache; return $cache->image_tag($matches[4], ($matches[1] ? $matches[1] : 0), ($matches[2] ? $matches[2] : 0), $matches[3]);'), $text);
4

2 に答える 2

3

代わりに、画像タグ(存在する場合)で余分なパラメータを取得しようとするこのような正規表現を試してみてください。このように、パラメーターは、含まれるパラメーターと省略されるパラメーターの任意の組み合わせを使用して、任意の順序にすることができます。

$string = 'this is some code and it has bbcode in it like [image width="740" height="249" parameters=""]51lca7dn56.jpg[/image] for example.';

if (preg_match('/\[image([^\]]*)\](.*?)\[\/image\]/i', $string, $match)) {
    var_dump($match);
}

結果の一致:

array(3) {
  [0]=>
  string(68) "[image width="740" height="249" parameters=""]51lca7dn56.jpg[/image]"
  [1]=>
  string(39) " width="740" height="249" parameters="""
  [2]=>
  string(14) "51lca7dn56.jpg"
}

したがって$match[1]、パラメータを調べて解析することができます。preg_replace_callbackコールバック内にロジックを実装するために使用する必要がある場合があります。

お役に立てば幸いです。

于 2012-07-27T00:36:46.250 に答える
2

正規表現を使用することをお勧めします

\[image\b(?=(?:[^\]]*\bwidth="(\d+)"|))(?=(?:[^\]]*\bheight="(\d+)"|))(?=(?:[^\]]*\bparameters="([^"]+)"|))[^\]]*\]([^\[]*)\[\/image\]

編集:

$string = 'this is some code and it has bbcode in it like [image width="740" height="249" parameters=""]51lca7dn56.jpg[/image] for example and [image parameters="" height="123" width="456"]12345.jpg[/image].';

if (preg_match_all('/\[image\b(?=(?:[^\]]*\bwidth="(\d+)"|))(?=(?:[^\]]*\bheight="(\d+)"|))(?=(?:[^\]]*\bparameters="([^"]+)"|))[^\]]*\]([^\[]*)\[\/image\]/i', $string, $match) > 0) {
    var_dump($match);
}

出力:

array(5) {
  [0]=>
  array(2) {
    [0]=>
    string(68) "[image width="740" height="249" parameters=""]51lca7dn56.jpg[/image]"
    [1]=>
    string(63) "[image parameters="" height="123" width="456"]12345.jpg[/image]"
  }
  [1]=>
  array(2) {
    [0]=>
    string(3) "740"
    [1]=>
    string(3) "456"
  }
  [2]=>
  array(2) {
    [0]=>
    string(3) "249"
    [1]=>
    string(3) "123"
  }
  [3]=>
  array(2) {
    [0]=>
    string(0) ""
    [1]=>
    string(0) ""
  }
  [4]=>
  array(2) {
    [0]=>
    string(14) "51lca7dn56.jpg"
    [1]=>
    string(9) "12345.jpg"
  }
}
于 2012-07-27T00:40:54.767 に答える