Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

close opened bb codes in php

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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]+)(?: .*)?(?<![/|/ ])]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading