0

I am currently experiencing a problem with regex although it has everything it needs, is there a regex lenght limit? Any help would be appreciated

$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)) {
DjEasyDick
  • 43
  • 4
  • 4
    what's up with that? why go through all that trouble if you want to strip all that. Why not just use `strip_tags()` with options? – Funk Forty Niner Mar 13 '17 at 17:42
  • 1
    Plus, the `` tag is deprecated/obsolete https://developer.mozilla.org/en-US/docs/Web/HTML/Element/font. You're probably stuck with using legacy code somewhere and having to replace all that, correct? – Funk Forty Niner Mar 13 '17 at 17:46
  • Striping tags wont help with my solution. Correct – DjEasyDick Mar 13 '17 at 17:51

1 Answers1

0

Your full pattern needs delimiters but you are adding them at the beginning and then imploding and concatenating, so you end up with many. Remove the delimiters from $possible_tags_in_regex and add them at the end:

$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/";

But you can eliminate all of the escaping by using something that won't be in the pattern such as ~:

$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~";
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87