Php 警告:preg#u replace():未知修饰符';[';

Php 警告:preg#u replace():未知修饰符';[';,php,regex,Php,Regex,假设我正在使用regex生成一个简单的BBCode函数,我希望[color=]标记在第一个分号处停止解析,以避免利用漏洞。现在,我在stackoverflow中看到了这个问题,但它从未显示如何实现它 $regex = '/\[colour\=/[^;]*/](.*?)\[\/colour\]/is'; $replace = '<span style="color: $1">$2</span>'; 跳出斜杠: $regex = '/\[colour\=\/[^;]*\/]

假设我正在使用regex生成一个简单的BBCode函数,我希望[color=]标记在第一个分号处停止解析,以避免利用漏洞。现在,我在stackoverflow中看到了这个问题,但它从未显示如何实现它

$regex = '/\[colour\=/[^;]*/](.*?)\[\/colour\]/is';
$replace = '<span style="color: $1">$2</span>';
跳出斜杠:

$regex = '/\[colour\=\/[^;]*\/](.*?)\[\/colour\]/is';
//            here __^    __^
或使用另一个分隔符:

$regex = '#\[colour=/[^;]*/](.*?)\[/colour\]#is';
但我想正则表达式是:

$regex = '#\[colour=/[^;]*\](.*?)\[/colour\]#is';
//       backslash here __^
根据更换零件:

$regex = '#\[colour=([^;]+)\](.*?)\[/colour\]#is';
$replace = '<span style="color: $1">$2</span>';
$regex='\[color=([^;]+)\](.*?\[/color\]\\\]is';
$replace='$2';
根据评论编辑:

$regex='\[color=(.*?\])(.*?\[/color\]is';
$replace='$2';
转义斜杠:

$regex = '/\[colour\=\/[^;]*\/](.*?)\[\/colour\]/is';
//            here __^    __^
或使用另一个分隔符:

$regex = '#\[colour=/[^;]*/](.*?)\[/colour\]#is';
但我想正则表达式是:

$regex = '#\[colour=/[^;]*\](.*?)\[/colour\]#is';
//       backslash here __^
根据更换零件:

$regex = '#\[colour=([^;]+)\](.*?)\[/colour\]#is';
$replace = '<span style="color: $1">$2</span>';
$regex='\[color=([^;]+)\](.*?\[/color\]\\\]is';
$replace='$2';
根据评论编辑:

$regex='\[color=(.*?\])(.*?\[/color\]is';
$replace='$2';
转义斜杠:

$regex = '/\[colour\=\/[^;]*\/](.*?)\[\/colour\]/is';
//            here __^    __^
或使用另一个分隔符:

$regex = '#\[colour=/[^;]*/](.*?)\[/colour\]#is';
但我想正则表达式是:

$regex = '#\[colour=/[^;]*\](.*?)\[/colour\]#is';
//       backslash here __^
根据更换零件:

$regex = '#\[colour=([^;]+)\](.*?)\[/colour\]#is';
$replace = '<span style="color: $1">$2</span>';
$regex='\[color=([^;]+)\](.*?\[/color\]\\\]is';
$replace='$2';
根据评论编辑:

$regex='\[color=(.*?\])(.*?\[/color\]is';
$replace='$2';
转义斜杠:

$regex = '/\[colour\=\/[^;]*\/](.*?)\[\/colour\]/is';
//            here __^    __^
或使用另一个分隔符:

$regex = '#\[colour=/[^;]*/](.*?)\[/colour\]#is';
但我想正则表达式是:

$regex = '#\[colour=/[^;]*\](.*?)\[/colour\]#is';
//       backslash here __^
根据更换零件:

$regex = '#\[colour=([^;]+)\](.*?)\[/colour\]#is';
$replace = '<span style="color: $1">$2</span>';
$regex='\[color=([^;]+)\](.*?\[/color\]\\\]is';
$replace='$2';
根据评论编辑:

$regex='\[color=(.*?\])(.*?\[/color\]is';
$replace='$2';

尝试以下代码片段:

$text = '[colour=red]Text in red[/colour]';
$regex = '/\[colour=([^;]*)\](.*?)\[\/colour\]/is';
$replace = '<span style="color: $1">$2</span>';

echo preg_replace($regex, $replace, $text);
$text='[color=red]红色文本[/color];
$regex='/\[color=([^;]*)\](.*?\[\/color\]/is';
$replace='$2';
echo preg_replace($regex,$replace,$text);

尝试以下代码片段:

$text = '[colour=red]Text in red[/colour]';
$regex = '/\[colour=([^;]*)\](.*?)\[\/colour\]/is';
$replace = '<span style="color: $1">$2</span>';

echo preg_replace($regex, $replace, $text);
$text='[color=red]红色文本[/color];
$regex='/\[color=([^;]*)\](.*?\[\/color\]/is';
$replace='$2';
echo preg_replace($regex,$replace,$text);

尝试以下代码片段:

$text = '[colour=red]Text in red[/colour]';
$regex = '/\[colour=([^;]*)\](.*?)\[\/colour\]/is';
$replace = '<span style="color: $1">$2</span>';

echo preg_replace($regex, $replace, $text);
$text='[color=red]红色文本[/color];
$regex='/\[color=([^;]*)\](.*?\[\/color\]/is';
$replace='$2';
echo preg_replace($regex,$replace,$text);

尝试以下代码片段:

$text = '[colour=red]Text in red[/colour]';
$regex = '/\[colour=([^;]*)\](.*?)\[\/colour\]/is';
$replace = '<span style="color: $1">$2</span>';

echo preg_replace($regex, $replace, $text);
$text='[color=red]红色文本[/color];
$regex='/\[color=([^;]*)\](.*?\[\/color\]/is';
$replace='$2';
echo preg_replace($regex,$replace,$text);

您应该始终首先验证正则表达式,例如在上您应该始终首先验证正则表达式,例如在上您应该始终首先验证正则表达式,例如在上您应该始终首先验证正则表达式,例如在上它们似乎都不起作用。它们都只是使[color]标记无法解析(更改为html标记)一点也不。“似乎没有任何效果”-然后正则表达式不匹配任何东西。因此,开始调试以找出原因。很抱歉,我不知道你所说的示例是什么意思。我的OP中的代码已经是我得到的。这似乎创建了以下内容:
[color=red;font size:50pt]用分号测试[color=blue]普通测试[/color][color=red;font size:50pt]分号测试[/color]”);
其中parseBBCode函数与我在OP中展示的差不多(但带有preg_replace return),它们似乎都不起作用。它们都只是使[color]标记根本不解析(更改为html标记)。“似乎都不起作用”-然后正则表达式没有匹配任何东西。所以开始调试以找出原因。很抱歉,我不知道你所说的示例是什么意思。我OP中的代码已经是我所拥有的了。这似乎产生了这样的结果:
[color=red;font size:50pt]用分号测试
echo parseBBCode('[color=blue]normal test[/color][color=red;font size:50pt]用分号[/color]”进行测试;
其中parseBBCode函数与我在OP中展示的函数非常相似(但使用preg_替换返回),它们似乎都不起作用。它们都只是使[color]标记根本不进行解析(更改为html标记)。“似乎都不起作用”-然后正则表达式没有匹配任何东西。所以开始调试以找出原因。很抱歉,我不知道你所说的示例是什么意思。我OP中的代码已经是我所拥有的了。这似乎产生了这样的结果:
[color=red;font size:50pt]用分号测试
echo parseBBCode('[color=blue]normal test[/color][color=red;font size:50pt]用分号[/color]”进行测试;
其中parseBBCode函数与我在OP中展示的函数非常相似(但使用preg_替换返回),它们似乎都不起作用。它们都只是使[color]标记根本不进行解析(更改为html标记)。“似乎都不起作用”-然后正则表达式没有匹配任何东西。所以开始调试以找出原因。很抱歉,我不知道你所说的示例是什么意思。我OP中的代码已经是我所拥有的了。这似乎产生了这样的结果:
[color=red;font size:50pt]用分号测试
echo parseBBCode('[color=blue]normal test[/color][color=red;font size:50pt]用分号[/color]'测试;
其中parseBBCode函数与我在OP中显示的函数非常相似(但带有preg_replace返回)