Php 如何使用preg_replace剥离字符,但仅当字符位于包装器中时

Php 如何使用preg_replace剥离字符,但仅当字符位于包装器中时,php,preg-replace,Php,Preg Replace,我需要从用户提供的输入中去掉'and'(单引号和双引号),但只有在它位于开括号和闭括号[]内时才需要这样做……我不想从字符串中去掉任何其他内容 因此: [font size=“10”] 需要换成 [字体大小=10] 但这是: [font size=10]母牛说“哞”[/font] 不会剥掉任何东西 这: [font size=“10”]母牛说“哞”[/font] 将更改为: [font size=10]母牛说“哞”[/font] 谢谢你的帮助……你的案子很简单;(: 带等号的字符串startig

我需要从用户提供的输入中去掉'and'(单引号和双引号),但只有在它位于开括号和闭括号[]内时才需要这样做……我不想从字符串中去掉任何其他内容

因此:

[font size=“10”]

需要换成

[字体大小=10]

但这是:

[font size=10]母牛说“哞”[/font]

不会剥掉任何东西

这:

[font size=“10”]母牛说“哞”[/font]

将更改为:

[font size=10]母牛说“哞”[/font]

谢谢你的帮助……

你的案子很简单;(:


带等号的字符串startig=后跟双引号/单引号,后跟空格或非空格…

我想到的快速变体(注意php 5.3语法):

您可以这样做:

(?>            # 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

$result=preg\u replace('~(?>\[\124;\ G(?很好的解决方案,这就是为什么我将regexp称为“只写”表达式:)
=[\'"]\s?([^\'"]*)\s?[\'"]
$s = preg_replace_callback('/(\\[[^\\]]+])/', function ($match) {
        return str_replace(['"', '\''], ['', ''], $match[1]);
    }, $s);
$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