Skip to content

Instantly share code, notes, and snippets.

@Langmans
Created February 11, 2016 09:45
Show Gist options
  • Select an option

  • Save Langmans/e3a162140e9ecd6267b9 to your computer and use it in GitHub Desktop.

Select an option

Save Langmans/e3a162140e9ecd6267b9 to your computer and use it in GitHub Desktop.
htmlify for stuff like youtube descriptions
<?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