Skip to content

Instantly share code, notes, and snippets.

@Altreus
Created September 7, 2010 09:54
Show Gist options
  • Save Altreus/568101 to your computer and use it in GitHub Desktop.
Save Altreus/568101 to your computer and use it in GitHub Desktop.
<?
function replace_h($matches) {
print_r($matches); die;
return $matches[1] . "\n" . str_repeat('-', length($matches[1]));
}
function replace_a($matches) {
return $matches[2] . " (" . $matches[1] . ") ";
}
$html = $_POST['html'];
$clean = strip_tags($html, "<h1><h2><h3><h4><p><a><td>");
$clean = preg_replace("/&nbsp;/", "", $clean);
$clean = preg_replace("/[\n\t]/", "", $clean);
$clean = html_entity_decode($clean);
$clean = preg_replace_callback("|<h([1-4]).*?>(.+?)</h\1>|", 'replace_h', $clean);
$clean = preg_replace_callback('|<a [^>]*?href="(.+?)"[^>]*?>(.+?)</a>|', 'replace_a', $clean);
$clean = preg_replace(':<(p|td)[^>]*?>(.+?)</\1>:', "\$2\n", $clean);
# Anything the stupid regexes missed.
$clean = strip_tags($clean);
$clean = preg_replace('/^\s+/m', '', $clean);
$clean = preg_replace("/\n/", "\n\n", $clean);
$clean = wordwrap($clean, 80);
header('Content-Type: text/plain');
print trim($clean);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment