Custom Markup Code - Part 1

Category: PHP
Reviewed by: Chiggins   
Reviewed on: Nov 01 2008
» Discuss this topic ( Posts)

 
function convert_markup( $sString ) {
    $string = $sString;
    
    // Markup conversion done here
    // Bold
    $string = preg_replace('/\[b\](.*?)\[\/b\]/is', '<strong>$1</strong>', $string);
    // Italicize
    $string = preg_replace('/\[i\](.*?)\[\/i\]/is', '<em>$1</em>', $string);
    // Underline
    $string = preg_replace('/\[u\](.*?)\[\/u\]/is', '<span style="text-decoration: underline;">$1</span>', $string);
    // Header 1
    $string = preg_replace('/\[h1\](.*?)\[\/h1\]/is' , '<h1>$1</h1>', $string);
    // Header 2
    $string = preg_replace('/\[h2\](.*?)\[\/h2\]/is' , '<h2>$1</h2>', $string);
    // Header 3
    $string = preg_replace('/\[h3\](.*?)\[\/h3\]/is' , '<h3>$1</h3>', $string);
    // Size
    $string = preg_replace('/\[size=(.*?)\](.*?)\[\/size\]/is' , '<span style="font-size: $1em;">$2</span>', $string);
    // Color
    $string = preg_replace('/\[color=(.*?)\](.*?)\[\/color\]/is' , '<span style="color: $1;">$2</span>', $string);
    // Create line breaks
    $string = nl2br( $string );
 
    // Output formatted text
    return $string;
}