Created
February 25, 2009 20:37
-
-
Save icco/70397 to your computer and use it in GitHub Desktop.
For converting plain text to html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
Basically is used to convert plain text to html. | |
@author Nat Welch | |
*/ | |
function htmlify($str) | |
{ | |
// replace special characters with HTML entities | |
$html = htmlspecialchars($str); | |
//paragraph-ise | |
$html = "<p>" . str_replace("\n\n", "</p>\n<p>", $html) . "</p>"; | |
// replace multiple spaces with single spaces | |
$html = preg_replace('/\s\s+/', ' ', $html); | |
// replace URLs with <a href...> elements | |
$html = preg_replace('/\s(\w+:\/\/)(\S+)/', ' <a href="\\1\\2" title="\\2">\\1\\2</a>', $html); | |
return $html; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment