2

preg_match と preg_replace について少し混乱しています。非常に長いコンテンツ文字列 (ブログから) があり、すべての [caption] タグを見つけて分離し、置き換えたいと考えています。可能なタグは次のとおりです。

[caption]test[/caption]
[caption align="center" caption="test" width="123"]<img src="...">[/caption]
[caption caption="test" align="center" width="123"]<img src="...">[/caption]

これが私が持っているコードです(しかし、私はそれが私が望むように機能していないことを発見しています...):

public function parse_captions($content) {
    if(preg_match("/\[caption(.*) align=\"(.*)\" width=\"(.*)\" caption=\"(.*)\"\](.*)\[\/caption\]/", $content, $c)) {
        $caption = $c[4];         
        $code = "<div>Test<p class='caption-text'>" . $caption . "</p></div>";
        // Here, I'd like to ONLY replace what was found above (since there can be
        // multiple instances
        $content = preg_replace("/\[caption(.*) width=\"(.*)\" caption=\"(.*)\"\](.*)\[\/caption\]/", $code, $content);
    }
    return $content;
}
4

2 に答える 2

1

目標は、コンテンツの位置を無視することです。これを試すことができます:

$subject = <<<'LOD'
[caption]test1[/caption]
[caption align="center" caption="test2" width="123"][/caption]
[caption caption="test3" align="center" width="123"][/caption]
LOD;

$pattern = <<<'LOD'
~
\[caption                          # begining of the tag 
(?>[^]c]++|c(?!aption\b))*         # followed by anything but c and ]
                                   # or c not followed by "aption"

(?|                                # alternation group
    caption="([^"]++)"[^]]*+]      # the content is inside the begining tag  
  |                                # OR
    ]([^[]+)                       # outside 
)                                  # end of alternation group

\[/caption]                        # closing tag
~x
LOD;

$replacement = "<div>Test<p class='caption-text'>$1</p></div>";

echo htmlspecialchars(preg_replace($pattern, $replacement, $subject));

パターン (要約版):

$pattern = '~\[caption(?>[^]c]++|c(?!aption\b))*(?|caption="([^"]++)"[^]]*+]|]([^[]++))\[/caption]~';

パターン説明:

タグの開始後]、キャプション属性の前にコンテンツを含めることができます。このコンテンツは次のように説明されています。

(?>                # atomic group
    [^]c]++        # all characters that are not ] or c, 1 or more times
  |                # OR
    c(?!aption\b)  # c not followed by aption (to avoid the caption attribute)
)*                 # zero or more times

代替グループ(?|は、同じ番号を持つ複数のキャプチャ グループを許可します。

(?|
       # case: the target is in the caption attribute #
    caption="      # (you can replace it by caption\s*+=\s*+")
    ([^"]++)       # all that is not a " one or more times (capture group)
    "
    [^]]*+         # all that is not a ] zero or more times

  |           # OR

       # case: the target is outside the opening tag #
    ]              # square bracket close the opening tag
    ([^[]+)        # all that is not a [ 1 or more times (capture group)
)

2 つのキャプチャが同じ番号 #1 を持つようになりました

注: 各キャプション タグが複数の行にないことが確実な場合は、パターンの最後に m 修飾子を追加できます。

注 2: すべての量指定子は所有格であり、迅速な失敗とパフォーマンスの向上が可能な場合は、アトミック グループを使用します。

于 2013-06-09T02:23:59.233 に答える