1

Im trying to get the bbcode image to work. But i have problems with it.

what in the code is wrong and why?

I have this input:

[img]http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg[/img]

and would like it to be:

<img src="http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg">

but the result is:

`Q: How can i make this work properly?

Full code:

 function parsebb($body,$userid = false, $user2 = false) {
            $find = array(
                "@\n@",
                "@[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]@is",
                "/\[url\=(.+?)\](.+?)\[\/url\]/is",
                "/\[b\](.+?)\[\/b\]/is",
                "/\[i\](.+?)\[\/i\]/is",
                "/\[u\](.+?)\[\/u\]/is",
                "/\[farge\=(.+?)\](.+?)\[\/farge\]/is",
                "/\[size\=(.+?)\](.+?)\[\/size\]/is",
                "/\[font\=(.+?)\](.+?)\[\/font\]/is",
                "/\[boks\=venstre\](.+?)\[\/boks\]/is",
                "/\[boks\=hoyre\](.+?)\[\/boks\]/is",
                "/\[boks\=midten\](.+?)\[\/boks\]/is",
                "/\[img\](.+?)\[\/img\]/is",
                "/\[email\](.+?)\[\/email\]/is",
                "/\[midten\](.+?)\[\/midten\]/is",
                "/\[venstre\](.+?)\[\/venstre\]/is",
                "/\[hoyre\](.+?)\[\/hoyre\]/is",
            );

            $replace = array(
                "<br />",
                "<a href=\"\\0\">\\0</a>",
                "<a href=\"$1\" target=\"_blank\">$2</a>",
                "<strong>$1</strong>",
                "<em>$1</em>",
                "<span style=\"text-decoration:underline;\">$1</span>",
                "<font color=\"$1\">$2</font>",
                "<font size=\"$1\">$2</font>",
                "<span style=\"font-family: $1\">$2</span>",
                "<div align=\"left\" style=\"text-align:center; width:50%; \">$1</div>",
                "<div align=\"right\" style=\"text-align:center; width:50%;\">$1</div>",
                "<div align=\"center\" style=\"text-align:center; width:50%;\">$1</div>",
                "<img src=\"$1\">",
                "<a href=\"mailto:$1\" target=\"_blank\">$1</a>",
                "<div style=\"text-align:center;\">$1</div>",
                "<div style=\"text-align:left;\">$1</div>",
                "<div style=\"text-align:right;\">$1</div>",


            );


                $body = htmlspecialchars($body);
                $body = preg_replace($specialCodes, $specialCodesReplace, $body);




            return $body;
        }
11
  • 3
    There are a lot of bbcode parsers out there. Commented Sep 30, 2015 at 13:24
  • 1
    Also, use some other delimiter; like ~ or @ instead of /. Commented Sep 30, 2015 at 13:25
  • 1
    The problem is at your "@[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]@is", pattern even [img] ... [/img] is matching with it just comment them and try again you will see it will be repleaced in well form or put this pattern line to the end of the array Commented Sep 30, 2015 at 13:35
  • 1
    Your server will get overloaded with such regex, also, you may consider using preg_quote instead of escaping every single block of code. Commented Sep 30, 2015 at 13:41
  • 2
    Instead of using <div align=\"center\" style=\"text-align:center; width:50%;\">$1</div> as replacement string, use for example <div class="center">$1</div> and define the CSS class .center (try to find an other name:) in a separate file. Two advantages of doing this: 1) your html code will be shorter. 2) the day you want to change how messages look like, you only need to edit the css classes. Commented Sep 30, 2015 at 13:52

1 Answer 1

1

The problem is at your

"@[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]@is"

pattern, even [img] ... [/img] is matching with it just, comment them and try again, it will be repleaced in well form or put this pattern line to the end of the array

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.