WRITE YOUR OWN BLOG ENGINE $replace = array(”",”", (Adelphia web hosting)
Tuesday, January 8th, 2008WRITE YOUR OWN BLOG ENGINE $replace = array(”",”
“, “
“); $text = str_replace($search, $replace, $text); return $text; } ?> The user-defined format function makes use of the PHP str_replace search and replace function to replace line breaks in your posts with
tags, and two line breaks in a row with paragraph tags. Because this function will be useful throughout your blog, save it to your blog root directory in a file called functions.php. Stepping through the script: function format($text) { The format function accepts an argument called $text, which will be the unformatted post pulled from the database: $text = “
” . $text . “
“; $text = stripslashes($text); First, the whole post text is wrapped inside paragraph tags and then any slashes are removed: $search = array(”\r”, “\n\n”, “\n”); $replace = array(”",”
“, “
“); $search is an array (think of it as a list) of the text we need to replace in the post. $replace is the corresponding array of text that will take its place. The first substitution performed is to strip out all carriage return characters, as line breaks on Windows machines are represented by two characters: a carriage return (\r) followed by a new line character (\n). Then double-line breaks are replaced with a closing and opening paragraph tag to create a new paragraph. The final substitution is to replace any remaining line breaks with
tags. $text = str_replace($search, $replace, $text); return $text; Finally, the search and replace arrays are passed along with the text to the str_replace function to perform the actual substitution and the resulting text is returned to wherever the function was called. Now call the functions.php include in your blog homepage by adding include (”functions.php”); to the PHP script just after the opening