0

ユーザーが入力した入力から ' と " (一重引用符と二重引用符) を取り除く必要がありますが、それが開き括弧と閉じ括弧 [ ] 内にある場合に限ります ... 文字列からそれ以外のものを取り除きたくありません。

したがって、この:

[フォントサイズ="10"]

に変更する必要があります

[フォントサイズ=10]

でもこれは:

[font size=10]牛は「モー」と言う[/font]

何も剥がしません。

これ:

[font size="10"]牛が「ムー」と言う[/font]

これに変わります:

[font size=10]牛は「モー」と言う[/font]

助けてくれてありがとう...

4

3 に答える 3

1

あなたはこれを行うことができます:

$result = preg_replace('~(?>\[|\G(?<!^)[^]"\']++)\K|(?<!^)\G["\']~', '', $string);

説明:

(?>            # open a group
    \[         # literal [
  |            # OR
    \G(?<!^)   # contiguous to a precedent match but not at the start of the string
    [^]"\']++  # all that is not quotes or a closing square bracket
)\K            # close the group and reset the match from results
|              # OR
(?<!^)\G["\']  # a contiguous quote

このパターンでは、括弧内の他のすべてのコンテンツが一致結果から削除されるため、引用符のみが置き換えられます。

于 2013-11-01T18:12:47.057 に答える
0

私の頭に浮かんだクイックバリアント(php 5.3構文に注意してください):

$s = preg_replace_callback('/(\\[[^\\]]+])/', function ($match) {
        return str_replace(['"', '\''], ['', ''], $match[1]);
    }, $s);
于 2013-11-01T17:48:56.757 に答える