Custom Markup Code - Part 1

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

Now, here are some markup codes you would put inside the function. The markup codes are found using Regular Expressions. Because explaining Regular Expressions would completely throw this article off course, I recommend looking them up on Regular-Expressions.info.

 
// 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);

Now there are a couple examples of markup code conversions. The preg_replace function searches for the first argument, replaces the first with the second argument, and the third argument is what should be searched.