Created
February 11, 2016 09:45
-
-
Save Langmans/e3a162140e9ecd6267b9 to your computer and use it in GitHub Desktop.
htmlify for stuff like youtube descriptions
This file contains hidden or 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 | |
| function htmlify($text) | |
| { | |
| $html = htmlspecialchars($text); | |
| $html = " <p> " . preg_replace("@(\r\n|\n|\r){2,}@", " </p> <p> ", $html) . " </p> "; | |
| $html = str_replace("\n", " <br> ", $html); | |
| $html = preg_replace('/\s\s+/', ' ', $html); | |
| $html = preg_replace_callback('/([a-z]+:\/\/)(\S+)/', function ($matches) { | |
| $matches[2] = preg_replace('@^www\.@i', '', $matches[2]); | |
| return sprintf(' <a href="%s" target="_blank">%s</a> ', trim($matches[0]), $matches[2], | |
| $matches[2]); | |
| }, $html); | |
| return $html; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment