Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Displaying Formatted User Input
Formatting with Custom Markup Tags
You can provide your own special markup tags for the user to use. For example, you can allow the to use [b]...[/b] for bolding, and [i]...[/i] for italics. Those would be simple string replace operations:
$output = str_replace("[b]", "<b>", $output);
$output = str_replace("[i]", "<i>", $output);
To get a little fancier, we will allow users to add links as well. For example, the user will be allowed to enter in [link="url"]...[/link], which we will turn into a proper <a href="">...</a> statement. We can't use a simple string replace here, instead we need to use regular expressions:
$output = ereg_replace('\[link="([[:graph:]]+)"\]', '<a href="\\1">', $output);
If you are unfamiliar with the ereg_replace() function, this statement means:
  • look for all occurrences of [link="..."] and replace it with <a href="...">
[[:graph:]] means any non-whitespace character. See the manual for more details about regular expressions.
The format_output() function in outputlib.php provides these markups and a few other ones as well. The general algorithm would be to:
  1. call htmlspecialchars() on the output text to convert all special characters to HTML entities
  2. systematically do string replacements or regexp replacements on our custom markup tags
<?php

function format_output($output) {
/****************************************************************************
* Takes a raw string ($output) and formats it for output using a special
* stripped down markup that is similar to HTML
****************************************************************************/

    
$output = htmlspecialchars(stripslashes($output));

    
/* new paragraph */
    
$output = str_replace('[p]', '&lt;p>', $output);

    
/* bold */
    
$output = str_replace('[b]', '&lt;b>', $output);
    
$output = str_replace('[/b]', '&lt;/b>', $output);

    
/* italics */
    
$output = str_replace('[i]', '&lt;i>', $output);
    
$output = str_replace('[/i]', '&lt;/i>', $output);

    
/* preformatted */
    
$output = str_replace('[pre]', '&lt;pre>', $output);
    
$output = str_replace('[/pre]', '&lt;/pre>', $output);

    
/* indented blocks (blockquote) */
    
$output = str_replace('[indent]', '&lt;blockquote>', $output);
    
$output = str_replace('[/indent]', '&lt;/blockquote>', $output);

    
/* anchors */
    
$output = ereg_replace('\[anchor=&amp;quot;([[:graph:]]+)&amp;quot;\]', '&lt;a name="\1">&lt;/a>', $output);
    
    
/* links, note we try to prevent javascript in links */
    
$output = str_replace('[link=&amp;quot;javascript', '[link=&amp;quot; javascript', $output);
    
$output = ereg_replace('\[link=&amp;quot;([[:graph:]]+)&amp;quot;\]', '&lt;a href="\1">', $output);
    
$output = str_replace('[/link]', '&lt;/a>', $output);      
    
    return
nl2br($output);
}

?>
[ Next Page ]

[Page 1]  [Page 2]  


Comments:
±â³×½ººÏ µµÀü, 100¹è »¡¶óÁö°í ½¬¿öÁø ¿µ¾î¿ø¸® ÀÌÈÆ±â11/15/05 22:34
RE: Unclosed Tags?Filipe02/21/05 19:50
½Å.¿ë.ºÒ.·®ÀÚ°¡ ¾Ë¾Æ¾ßÇÒ Á¤.º¸/´ë°ø°³ÇÏÁö¿¬12/28/04 02:21
Ä«/µå/µ¹·Á¸·±â·Î/¸Á°¡Áö½ÅºÐ/²À º¸¼¼¿ä!ÀÌÇýÁø12/07/04 06:18
Ä«,µå,¿¬,ü,ÀÚ/¿¹.Á¤.ÀÚ ´ë,Ãâ 100-1000¸¸¿øÀÌ´ÙÇö12/05/04 06:47
´ë'Ãâ'°Å'Àý'½Ã'100%µÇ'°Ô'ÇÏ'´Â'¹æ'¹ýÇѰæ¿í12/04/04 21:45
½Å.¿ë.ºÒ.·®.ÀÚ/´çÀÏ500/´ë.Ãâ.ºñ.¹ýÀÌÈñÁø12/02/04 09:27
½Å.¿ë.ºÒ.·®/Ä«.µå.¿¬.ü/´ë.Ãâ/È¥ÀÚ/ÇØ.°áÇÏ´Â/¹æ.¹ý±èÇö¼­11/27/04 23:55
½Å.¿ë.ºÒ.·®.ÀÚ/´çÀÏ500/´ë.Ãâ.ºñ.¹ýÀÌÈñÁø11/27/04 12:32
´ë'Ãâ'°Å'Àý'½Ã'100%µÇ'°Ô'ÇÏ'´Â'¹æ'¹ýÇѰæ¹Î11/23/04 11:11
½Å.¿ëºÒ.·®ÀÚ°¡ ¾Ë¾Æ¾ßÇÒ Á¤.º¸ ´ë.°ø.°³ ÀÌ´ÙÁø11/22/04 03:08
Ä«/µå/µ¹·Á¸·±â·Î/¸Á°¡Áö½ÅºÐ/²À º¸¼¼¿ä!ÀÌÁ¤¿¬11/18/04 13:03
5.ºÐ.¸¸¿¡ 4.0¸¸.¿ø ¹«,ÀÌ.ÀÚ·Î ºô.¸®±âÁö¿µÈñ11/17/04 12:43
Ä«.µå.±ø.¾È.ÇÏ.°í.µ·.¸¸.µé.¾î.¾².´Â.ºñ.¹ýÀÌÈñÁø11/15/04 10:25
RE: off topicAmo10/29/03 19:40
Thanks Yingbacaribaro08/05/03 12:50
multiple page forms / formatting outputJose candelaria10/24/02 22:29
Use real HTML in input, but safelyBastiaan09/22/02 04:19
Database Driven Version in PHPRobert Taylor (Manix)08/20/02 16:15
RE: this is exactly what i wanted to knowTony Reid07/21/02 16:35
RE: saving user input in databasecraig jardine05/04/02 08:28
YOU ROCK!Ernest Correale05/01/02 10:54
SQL Error 1064 when $output contains a 'James Wyld04/25/02 22:12
off topicjeff03/12/02 23:50
replace, but not between <t>hes</e>Tsunami01/21/02 16:00
this is great!Leah Yates12/20/01 15:36
RE: parenthesisRockMonkey10/28/01 20:47
parenthesisandrew b08/27/01 15:14
saving user input in databaserishabh gupta08/16/01 10:15
Easy WayDarren Valentine07/27/01 05:26
RE: JavaScript Auto Formatting?Richard06/27/01 06:25
html<->pseudojuozas salna05/07/01 07:14
General script for replacing pseudo-markupJakob Persson04/27/01 09:13
RE: Generating linksVargo03/17/01 08:59
Generating linksLeo Miyagi02/19/01 10:23
php to call data neededlei02/11/01 01:07
UBB code translation functionZulu12/01/00 05:52
JavaScript Auto Formatting?Frank11/27/00 06:02
RE: yeah , but i'm slow like turtleOliwier09/30/00 18:20
RE: mouseovers in linksYing Zhang09/05/00 00:40
mouseovers in linksJoe Sheble08/30/00 18:30
Editing formated codeJ. Kobinski08/06/00 15:17
RE: Unclosed Tags?Cédric CHERCHI08/01/00 12:01
RE: yeah , but i'm slow like turtleSean Pecor07/25/00 20:05
RE: yeah , but i'm slow like turtleAdrian Kubala07/25/00 12:15
RE: str_tagsYing Zhang07/24/00 05:05
RE: Unclosed Tags?Keita Ito07/23/00 18:01
RE: instead of custom markup languages...Keita Ito07/23/00 17:56
RE: instead of custom markup languages...Keita Ito07/23/00 17:53
RE: instead of custom markup languages...Ben Munoz07/21/00 14:01
instead of custom markup languages...Ben Munoz07/21/00 13:56
Unclosed Tags?Brian Mertens07/21/00 00:44
yeah , but i'm slow like turtlephilip olson07/21/00 00:33
SimilarMike Hall07/20/00 05:11
RE: An additionVincent Vollers07/20/00 04:52
RE: An additionDoug MacDougall07/20/00 01:18
Just use HTML.. life is complicated enoughDoug MacDougall07/20/00 01:06
RE: str_tagsSteve Yelvington07/19/00 19:45
An additionVincent Vollers07/19/00 13:18
str_tagsMatt07/19/00 11:45
this is exactly what i wanted to knowphilip olson07/19/00 00:17
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.