BBCode: use 'preg_replace()' instead of 'str_replace()' to prevent unclosed tags BBCode: use 'preg_replace()' instead of 'str_replace()' to prevent unclosed tags
 

News:

CPG Release 1.6.26
Correct PHP8.2 issues with user and language managers.
Additional fixes for PHP 8.2
Correct PHP8 error with SMF 2.0 bridge.
Correct IPTC supplimental category parsing.
Download and info HERE

Main Menu

BBCode: use 'preg_replace()' instead of 'str_replace()' to prevent unclosed tags

Started by Αndré, July 18, 2008, 01:33:08 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Αndré

Hi,

i changed 'include/functions.inc.php' to use regular expression replace instead of string replace. The advantage is, that unclosed tags will not be processed.
Additionally i added some new bbcode tags.

Open 'include/functions.inc.php' and find:
function bb_decode($text)
{


Comment out:
        // [b] and [/b] for bolding text.
        $text = str_replace("[b]", '<b>', $text);
        $text = str_replace("[/b]", '</b>', $text);

        // [u] and [/u] for underlining text.
        $text = str_replace("[u]", '<u>', $text);
        $text = str_replace("[/u]", '</u>', $text);

        // [i] and [/i] for italicizing text.
        $text = str_replace("[i]", '<i>', $text);
        $text = str_replace("[/i]", '</i>', $text);

        // colours
        $text = preg_replace("/\[color=(\#[0-9A-F]{6}|[a-z]+)\]/", '<span style="color:$1">', $text);
        $text = str_replace("[/color]", '</span>', $text);


and add:
        //preg_replace() instead of str_replace() to prevent unclosed tags
        $text = preg_replace("/\[b\](.*)\[\/b\]/Usi", "<b>\\1</b>", $text);
        $text = preg_replace("/\[u\](.*)\[\/u\]/Usi", "<u>\\1</u>", $text);
        $text = preg_replace("/\[i\](.*)\[\/i\]/Usi", "<i>\\1</i>", $text);
        $text = preg_replace("/\[color=(\#[0-9A-F]{6}|[a-z]+)\](.*)\[\/color\]/Usi", "<span style=\"color:\\1\">\\2</span>", $text);

        //some new tags
        $text = preg_replace("/\[s\](.*)\[\/s\]/Usi", "<span style=\"text-decoration: line-through\">\\1</span>", $text);
        $text = preg_replace("/\[size=(.*)\](.*)\[\/size\]/Usi", "<span style=\"font-size:\\1ex\">\\2</span>", $text);
        $text = preg_replace("/\[quote](.*)\[\/quote\]/Uis", "<div>Quote:</div><div style=\"border:solid 1px;\">\\1</div>", $text);
        $text = preg_replace("/\[quote=(.*)](.*)\[\/quote\]/Uis", "<div>Quote from: \\1</div><div style=\"border:solid 1px;\">\\2</div>", $text);


Keep in mind, that the language of the quote tag is hard-coded yet.


-
muu

Αndré

If you want to allow your users to embed youtube videos (e.g. in comments), add
        $text = preg_replace("/\[youtube\](.*)youtube.com\/watch\?v=(.*)\[\/youtube\]/Usi", "<object width=\"425\" height=\"344\"><param name=\"movie\" value=\"http://www.youtube.com/v/\\2&hl=de&fs=1\"></param><param name=\"allowFullScreen\" value=\"true\"></param><embed src=\"http://www.youtube.com/v/\\2&hl=de&fs=1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" width=\"425\" height=\"344\"></embed></object>", $text);

after
        //some new tags
        $text = preg_replace("/\[s\](.*)\[\/s\]/Usi", "<span style=\"text-decoration: line-through\">\\1</span>", $text);
        $text = preg_replace("/\[size=(.*)\](.*)\[\/size\]/Usi", "<span style=\"font-size:\\1ex\">\\2</span>", $text);
        $text = preg_replace("/\[quote](.*)\[\/quote\]/Uis", "<div>Quote:</div><div style=\"border:solid 1px;\">\\1</div>", $text);
        $text = preg_replace("/\[quote=(.*)](.*)\[\/quote\]/Uis", "<div>Quote from: \\1</div><div style=\"border:solid 1px;\">\\2</div>", $text);

keola56

how would i go about making a custom BBCode entry for something like myspace profiles where i can have users just add a code for example [myspace]name[/myspace] and have the code auto fill http://www.myspace.com/name but only display the name inputted to be click able?

Αndré

Quote from: keola56 on December 04, 2008, 10:30:14 AM
how would i go about making a custom BBCode entry for something like myspace profiles where i can have users just add a code for example [myspace]name[/myspace] and have the code auto fill http://www.myspace.com/name but only display the name inputted to be click able?

        $text = preg_replace("/\[myspace\](.*)\[\/myspace\]/Usi", "<a href=\"http://www.myspace.com/\\1\">\\1</a>", $text);

keola56

wow thanks you are awesome!

works perfectly.

now i got a question i would like to ask privately because i have this idea i would like to implement, is this possible? sorry for such a personal request I do appreciate your help  8)

ultrabr


Hi guys . I am unable to adjust my script, returns an error  :( 

what should I replace for preg_replace(); ?

Please, help me !

See:

$exp1=ereg_replace("([0-9]*).*","\\1",$exp);
$exp2=ereg_replace("[0-9]*([D|H|M|W|Y])","\\1",$exp);

$domaine = ereg_replace(".*@(.*)","\\1",$from);
$ademail=trim(ereg_replace("[^<]*<([^>]*)>","\\1",$email));



  $from=ereg_replace("[^<]*<([^>]*)>","\\1",$from);
        if(!preg_match('#^[\w.-]+@[\w.-]+\.[a-z]{2,5}$#i',$ademail)) {
            return FALSE;
        }


Thanks !



Αndré