我目前正在遇到正则表达式的问题,虽然它有它需要的一切,是否有正则表达式长度限制?任何帮助将不胜感激
$possible_tags_in_regex = "/(<b>|<\/b>|<i>|<\/i>|<u>|<\/u>|<tt>|<\/tt>|<font size=[1-7]>|<font color=#[a-fA-F0-9]>|<\/font>)*/";
// Add possible tags between every character in a string
$regex = implode($possible_tags_in_regex, str_split($regex));
$regex = $possible_tags_in_regex.$regex.$possible_tags_in_regex;
// Format every regex every match with given tags
if (preg_match_all($regex, $input, $to_be_replaced)) {
答案 0 :(得分:0)
您的完整模式需要分隔符,但您在开头添加它们然后进行内爆和连接,因此您最终会遇到许多分隔符。从$possible_tags_in_regex
中删除分隔符并在结尾处添加分隔符:
$possible_tags_in_regex = "(<b>|<\/b>|<i>|<\/i>|<u>|<\/u>|<tt>|<\/tt>|<font size=[1-7]>|<font color=#[a-fA-F0-9]>|<\/font>)*";
// Add possible tags between every character in a string
$regex = implode($possible_tags_in_regex, str_split($regex));
$regex = "/$possible_tags_in_regex.$regex.$possible_tags_in_regex/";
但是你可以通过使用不会出现在~
等模式中的东西来消除所有的逃避:
$possible_tags_in_regex = "(<b>|</b>|<i>|</i>|<u>|</u>|<tt>|</tt>|<font size=[1-7]>|<font color=#[a-fA-F0-9]>|</font>)*";
// Add possible tags between every character in a string
$regex = implode($possible_tags_in_regex, str_split($regex));
$regex = "~$possible_tags_in_regex.$regex.$possible_tags_in_regex~";