Home > Blockchain >  close opened bb codes in php
close opened bb codes in php

Time:09-07

I found the following code on gitbub which closes opened arrow tags like this <code>.

I need to close opened square brackets bbcodes like this: [code]

function closetags($html) {
        preg_match_all('#<([a-z] )(?: .*)?(?<![/|/ ])>#iU', $html, $result);
        $openedtags = $result[1];
        preg_match_all('#</([a-z] )>#iU', $html, $result);
        
        $closedtags = $result[1];
        $len_opened = count($openedtags);
    
        if (count($closedtags) == $len_opened) {
            return $html;
        }
        $openedtags = array_reverse($openedtags);
        for ($i=0; $i < $len_opened; $i  ) {
            if (!in_array($openedtags[$i], $closedtags)) {
                $html .= '</'.$openedtags[$i].'>';
            } else {
                unset($closedtags[array_search($openedtags[$i], $closedtags)]);
            }
        }
        return $html;
    }

I tried to modify the function like this:

function closetags($html) {
        preg_match_all('#[([a-z] )(?: .*)?(?<![/|/ ])]#iU', $html, $result);
        $openedtags = $result[1];
        preg_match_all('#[/([a-z] )]#iU', $html, $result);
        
        $closedtags = $result[1];
        $len_opened = count($openedtags);
    
        if (count($closedtags) == $len_opened) {
            return $html;
        }
        $openedtags = array_reverse($openedtags);
        for ($i=0; $i < $len_opened; $i  ) {
            if (!in_array($openedtags[$i], $closedtags)) {
                $html .= '[/'.$openedtags[$i].']';
            } else {
                unset($closedtags[array_search($openedtags[$i], $closedtags)]);
            }
        }
        return $html;
    }

But it doesn't work.

echo closetags('How to close [code]this BB code auto?');

What would I do?

The results:

Warning: preg_match_all(): Compilation failed: unmatched closing parenthesis at offset 8 in C:\xampp\htdocs\bboard.ge\public\app\classes\BBCode.php on line 9

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\bboard.ge\public\app\classes\BBCode.php on line 10

Warning: preg_match_all(): Compilation failed: unmatched closing parenthesis at offset 9 in C:\xampp\htdocs\bboard.ge\public\app\classes\BBCode.php on line 11

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\bboard.ge\public\app\classes\BBCode.php on line 13

Warning: count(): Parameter must be an array or an object that implements Countable in C:\xampp\htdocs\bboard.ge\public\app\classes\BBCode.php on line 14

Warning: count(): Parameter must be an array or an object that implements Countable in C:\xampp\htdocs\bboard.ge\public\app\classes\BBCode.php on line 16

How to close [code]this BB code auto?

CodePudding user response:

You need to escape the [s are these start character classes when not escaped.

so

[/([a-z] )]

should be

\[([a-z] )]

and

[([a-z] )(?: .*)?(?<![/|/ ])]

should be:

\[([a-z] )(?: .*)?(?<![/|/ ])]

CodePudding user response:

Finally got it. Thanks everyone.

function closetags($html) {
        preg_match_all('#\[([a-z] )(?: .*)?(?<![/|/ ])]#iU', $html, $result);
        $openedtags = $result[1];
        preg_match_all('#\[/([a-z] )]#iU', $html, $result);
        
        $closedtags = $result[1];
        $len_opened = count($openedtags);
    
        if (count($closedtags) == $len_opened) {
            return $html;
        }
        $openedtags = array_reverse($openedtags);
        for ($i=0; $i < $len_opened; $i  ) {
            if (!in_array($openedtags[$i], $closedtags)) {
                $html .= '[/'.$openedtags[$i].']';
            } else {
                unset($closedtags[array_search($openedtags[$i], $closedtags)]);
            }
        }
        return $html;
    }
  • Related