Jump to content

Magic Quotes- Only for certain bits of text.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
Hello,
While thinking of making a forum one of he problems i can up with was:
How to let the user format the tyext while stopping bad code???
I also discovered that text box input uses /n instead of <br>. So my to questions:
What would be the best way to let certain code through?
My idea was to use magic quotes (or something like it) to convert <br> into <br> then turn
certain bits back into <br> with the replace() (or something like it) function. Is their an easier way?

Thanks in advanced (I have read "common php questions" by Nullworm- but found the bit about this a bit under covered- I understand it was just a reference so we know what to ask for).

EDIT: Just remebered what i was going to say:
How do i convert /n to <br> (YES I DID READ THAT PART FROM NULLWORM).
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
that's why forums usually use BBcode, as it's just not html-tags, but almost. safer for all.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts
If I may advise you, save yourself the time and trouble and just get this :

TinyMCE - Home

:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it

#4
Takumi

Takumi

    Newbie

  • Members
  • PipPip
  • 16 posts
If you do want to do it on your own, take a look at this function:

preg_replace($pattern, $replacement, $string);

PHP: preg_replace - Manual

Takumi

#5
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
@ORjANT
Yes I guess it is like BBCode any pointers??
@DEViAnT
Yes but is that going to slide into my site perfectly and intergrate keenly with my current login system. Do you have any idea how strange it seems when forums dedicated to coding are made using a code free installer???

Besides I'm having tons of fun making it.

@Takumi
+rep +rep Thanks much
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#6
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
a pointer for BBcode is actually to php it self... PHP: BBCode - Manual
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Actually if you're interested in regular expressions for the matter, I wrote up a quick function more or less 7 years ago when learning regular expressions.
function parse_bbcode($text, $smileys = true)
{
    $text = htmlentities($text);
    $tags = array(
        '#\[b\](.*?)\[/b\]#si' => '<strong>\\1</strong>',
        '#\[i\](.*?)\[/i\]#si' => '<em>\\1</em>',
        '#\[u\](.*?)\[/u\]#si' => '<u>\\1</u>',
        '#\[s\](.*?)\[/s\]#si' => '<strike>\\1</strike>',
        '#\[color=(.*?)\](.*?)\[/color\]#si' => '<font color="\\1">\\2</font>',
        '#\[img\](.*?)\[/img\]#si' =>  '<img src="\\1" border="0" alt="" />',
        '#\[url=(.*?)\](.*?)\[/url\]#si' => '<a href="\\1">\\2</a>',
        '#\[email\](.*?)\[/email\]#si' => '<a href="mailto:\\1" title="Email \\1">\\1</a>',
        '#\[code\](.*?)\[/code\]#si' => '<blockquote style="font-face: \'trebuchet ms\'; 40px;margin:6px;color:#575757;margin-left:20px;">\\1</blockquote>',
        '#\[align=(.*?)\](.*?)\[/align\]#si' => '<div style="text-align: \\1;">\\2</div>',
        '#\[br\]#si' => '<br style="clear: both;" />',
    );

    foreach ($tags as $search => $replace){
        $text = preg_replace($search, $replace, $text);
    }
    
    if ($smiley == true) {
       $smilies = array(
            ':)' => '<img src="images/smilies/smile.gif" border="0" height="16" width="16" alt="Smile" title="Smile" />',
            ':(' => '<img src="images/smilies/sad.gif" border="0" height="16" width="16" alt="Sad" title="Sad" />',
            ':D' => '<img src="images/smilies/biggrin.gif" border="0" height="16" width="16" alt="Big Grin" title="Big Grin" />',
            ':S' => '<img src="images/smilies/confused.gif" border="0" height="16" width="16" alt="Confused" title="Confused" />'
            // more here
        );

        foreach ($smilies as $search => $replace){
               $text = str_replace($search, $replace, $text);
        }
    }
    
    //nl2br allows newlines to be converted to quotes
    return nl2br($text);
}
I left the smiley support in, as it shows how to do simple translations.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#8
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts

bbqroast said:

@DEViAnT
Yes but is that going to slide into my site perfectly and intergrate keenly with my current login system. Do you have any idea how strange it seems when forums dedicated to coding are made using a code free installer???

The text editor can be completely customized down to the bone to look and function exactly how you would want it to. Add your own buttons, change the display, enable/disable what you want, etc.

If you wanna learn, then go for it. If your pressed for time, you might as well use it.

:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it

#9
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
Look i have already started and i am going fine.
Thanks NULLW0RM (p.s. where did your spinny worm thing go???)
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).