Php Regex-删除[image=][/image]BB标记之间的四个特殊字符

Php Regex-删除[image=][/image]BB标记之间的四个特殊字符,php,regex,Php,Regex,在脚本中,我有一个BB图像代码,可以有以下两种格式之一: [image=http://i459.photobucket.com/albums/xxx/enkidu-wd/test/38602_450pom.jpg][/image] (without Alt text) [image=https://m.photobucket.com/enkidu-wd/test/38602_450pomnik.jpg]alt text[/image] (with Alt text) 我的目标是始终删除这四个

在脚本中,我有一个BB图像代码,可以有以下两种格式之一:

[image=http://i459.photobucket.com/albums/xxx/enkidu-wd/test/38602_450pom.jpg][/image] (without Alt text)

[image=https://m.photobucket.com/enkidu-wd/test/38602_450pomnik.jpg]alt text[/image] (with Alt text)
我的目标是始终删除这四个字符:
$&'
-但只有在将这些字符作为alt文本添加时(即在
[image=…]alt text[/image]
之间)。如果这些字符出现在$text消息中的
[image][/image]
标记之外的任何位置,则应忽略(而不是删除)

我尝试按如下所示使用preg_replace,但它不起作用。请注意,$text是一条消息,可能包含也可能不包含此[image][/image]标记

$start = '\[image=';
$end  = '\[\/image\]';
$text = preg_replace(
    '#('.$start.')(.*)('.$end.')#i',
    '$1'.str_replace(array('$','&','\'','"'),'').(*).'$3',
    $text
);

您可以尝试以下方法:

$pattern = '~(?:\G(?!\A)|[image\b[^]]*])[^[$&\'"]*\K[$&\'"]~i';
$pattern = '~(?:\G(?!\A)|[image\b[^]]*])[^[$&\'"]*(?:\[(?!/image\b)[^][]*][^[$&\'"]*)*\K[$&\'"]~i';
如果图像标记之间可以包含其他BBCODE标记,则可以如下更改:

$pattern = '~(?:\G(?!\A)|[image\b[^]]*])[^[$&\'"]*\K[$&\'"]~i';
$pattern = '~(?:\G(?!\A)|[image\b[^]]*])[^[$&\'"]*(?:\[(?!/image\b)[^][]*][^[$&\'"]*)*\K[$&\'"]~i';
使用:

详情:

~ # pattern delimiter
(?:
    \G(?!\A) # contiguous to the previous match, not at the start of the string
  | # OR
    [image\b[^]]*] # an opening image tag
)
[^[$&\'"]* #"# all that isn't an opening square bracket or one the chars
\K # remove all that has been matched before from the match result
[$&\'"]
~i
说明:


由于
[^[$&\'”]*
禁止打开方括号,一旦到达关闭的
[/image]
,连续性就会中断,
\G
锚定失败。继续的唯一方法是找到另一个打开的
[image…]
标记。

您可以尝试以下操作:

$pattern = '~(?:\G(?!\A)|[image\b[^]]*])[^[$&\'"]*\K[$&\'"]~i';
$pattern = '~(?:\G(?!\A)|[image\b[^]]*])[^[$&\'"]*(?:\[(?!/image\b)[^][]*][^[$&\'"]*)*\K[$&\'"]~i';
如果图像标记之间可以包含其他BBCODE标记,则可以如下更改:

$pattern = '~(?:\G(?!\A)|[image\b[^]]*])[^[$&\'"]*\K[$&\'"]~i';
$pattern = '~(?:\G(?!\A)|[image\b[^]]*])[^[$&\'"]*(?:\[(?!/image\b)[^][]*][^[$&\'"]*)*\K[$&\'"]~i';
使用:

详情:

~ # pattern delimiter
(?:
    \G(?!\A) # contiguous to the previous match, not at the start of the string
  | # OR
    [image\b[^]]*] # an opening image tag
)
[^[$&\'"]* #"# all that isn't an opening square bracket or one the chars
\K # remove all that has been matched before from the match result
[$&\'"]
~i
说明:



由于
[^[$&\'”]*
禁止打开方括号,一旦到达关闭位置
[/image]
,连续性将被破坏,
\G
锚定将失败。继续的唯一方法是找到另一个打开位置
[image…]
tag.

删除字符的原因是什么?我认为更好的做法是对其进行清理,否则可能会破坏用户的原始意图。原因是“安全”,即当用户输入其中一个字符时,BB代码图像不起作用。不确定原因,但这是第三方脚本。他们说这是为了安全啊,好吧,如果那是他们的借口\_(ツ)_/“@Tom请尝试使用我的库短代码来解析短代码。删除字符的原因是什么?我认为更好的做法是对它们进行清理,否则可能会破坏用户的原始意图。原因是“安全”,即当用户输入其中一个字符时,BB代码图像不起作用。不确定是的,但这是第三方脚本。他们说这是为了安全。啊,好吧。如果这是他们的借口
\_(ツ)_/“
@Tom请尝试我的库短码来解析短码。对不起-如果我还想从alt文本中删除:[],我应该添加什么?我尝试了:$pattern='~(?:\G(?!\A)|[image\b[^]*][^[]$&'“]*\K[$&'”]~I';但是它不起作用了吗?@Tom采取第一种模式并将
[^[$&'“]*\K[$\\\\\\\\\\\\]]
更改为
[*-[*\\\\\\\[]]$&\'“]\[(?!/image\b))
(在pcre中的字符类中,如果不想转义结束方括号
,请将其放在类的开头或反运算
^
)之后,好的,它应该是:?$pattern='~(?:\G(?!\a)[image\b[^]*])[^][$&']*\K(?[[]$$$&'']。[(?!/image\b))~i';也许吧,但我真的看不见,因为你不使用格式(在代码周围加上严肃的重音)@Tom:你忘了避开文字开头的括号:
$pattern='~(?:\G(?!\A)\[image\b[^]*][^][$&']*\K(?:[$&'']|\[(‌​?!‌​/image\b);
对不起-如果我还想从alt文本中删除:[],我应该添加什么呢?我尝试了:$pattern='~(?:\G(?)\A)[image\b[^]*][^[[$&\'”]*\K[$&\'“]~i';但它不起作用?@Tom:使用第一个模式,并将
[^[$&\\\\']*\K[$&'”][$\\\\\\\\]]//code>更改为
[^[$-[$&\\\\\\\\\\\\\\\\\]-[-[$++](在pcre中的字符类中,如果您不想转义结束方括号
]
,请将其放在类的开头或求反后
^
)好的,所以它应该是:?$pattern='~(?:\G(?!\a)|[image\b[^]*][^][$&'']*\K(?:[$&''''].[[(?!/image\b))~i';可能吧,但我看不到,因为您不使用格式设置(在代码周围放上严肃的口音)@Tom:你忘了逃出字面的开头括号:
$pattern='~(?:\G(?!\A)\[image\b[^]]*][^][$&\']]*\K(?[]$&\']|\[(‌​?!‌​/image\b))~i';