Advanced search  

News:

cpg1.5.22 maintenance release - upgrade recommended
The Coppermine development team is releasing an update for Coppermine in order to fix several minor issues. All fixes are not security critical, so if your gallery is running fine with cpg1.5.20 you don't need to upgrade. If you are running an older version than cpg1.5.20, you must update to this latest version as soon as possible because of the security impact!
[more]

Pages: [1]   Go Down

Author Topic: BBCode: use 'preg_replace()' instead of 'str_replace()' to prevent unclosed tags  (Read 19636 times)

0 Members and 1 Guest are viewing this topic.

Αndré

  • Administrator
  • Coppermine addict
  • *****
  • Country: de
  • Offline Offline
  • Gender: Male
  • Posts: 11799

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:
Code: [Select]
function bb_decode($text)
{

Comment out:
Code: [Select]
        // [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:
Code: [Select]
        //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
Logged

Αndré

  • Administrator
  • Coppermine addict
  • *****
  • Country: de
  • Offline Offline
  • Gender: Male
  • Posts: 11799

If you want to allow your users to embed youtube videos (e.g. in comments), add
Code: [Select]
        $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
Code: [Select]
        //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);
Logged

keola56

  • Coppermine newbie
  • Offline Offline
  • Posts: 3

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?
Logged

Αndré

  • Administrator
  • Coppermine addict
  • *****
  • Country: de
  • Offline Offline
  • Gender: Male
  • Posts: 11799

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?

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

keola56

  • Coppermine newbie
  • Offline Offline
  • Posts: 3

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)
Logged
Pages: [1]   Go Up
 

Page created in 0.154 seconds with 21 queries.