| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
function html2text( $badStr ) { |
|---|
| 21 |
|
|---|
| 22 |
$is_open_tb = false; |
|---|
| 23 |
$is_open_dq = false; |
|---|
| 24 |
$is_open_sq = false; |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
while (substr_count($badStr, '<!--') && |
|---|
| 29 |
substr_count($badStr, '-->') && |
|---|
| 30 |
strpos($badStr, '-->', strpos($badStr, '<!--' ) ) > strpos( $badStr, '<!--' ) ) { |
|---|
| 31 |
$badStr = substr( $badStr, 0, strpos( $badStr, '<!--' ) ) . |
|---|
| 32 |
substr( $badStr, strpos( $badStr, '-->', |
|---|
| 33 |
strpos( $badStr, '<!--' ) ) + 3 ); |
|---|
| 34 |
} |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
$len = strlen($badStr); |
|---|
| 39 |
$chr = $badStr{0}; |
|---|
| 40 |
$goodStr = ''; |
|---|
| 41 |
|
|---|
| 42 |
if ($len > 0) { |
|---|
| 43 |
for ($x=0; $x < $len; $x++ ) { |
|---|
| 44 |
$chr = $badStr{$x}; |
|---|
| 45 |
switch ( $chr ) { |
|---|
| 46 |
case '<': |
|---|
| 47 |
if ( !$is_open_tb && strtolower( substr( $badStr, $x + 1, 5 ) ) == 'style' ) { |
|---|
| 48 |
$badStr = substr( $badStr, 0, $x ) . |
|---|
| 49 |
substr( $badStr, strpos( strtolower( $badStr ), '</style>', $x ) + 7 ); |
|---|
| 50 |
$chr = ''; |
|---|
| 51 |
} else if ( !$is_open_tb && strtolower( substr( $badStr, $x + 1, 6 ) ) == 'script' ) { |
|---|
| 52 |
$badStr = substr( $badStr, 0, $x ) . |
|---|
| 53 |
substr( $badStr, strpos( strtolower( $badStr ), '</script>', $x ) + 8 ); |
|---|
| 54 |
$chr = ''; |
|---|
| 55 |
} else if (!$is_open_tb) { |
|---|
| 56 |
$is_open_tb = true; |
|---|
| 57 |
} else { |
|---|
| 58 |
$chr = '<'; |
|---|
| 59 |
} |
|---|
| 60 |
break; |
|---|
| 61 |
|
|---|
| 62 |
case '>': |
|---|
| 63 |
if ( !$is_open_tb || $is_open_dq || $is_open_sq ) { |
|---|
| 64 |
$chr = '>'; |
|---|
| 65 |
} else { |
|---|
| 66 |
$is_open_tb = false; |
|---|
| 67 |
} |
|---|
| 68 |
break; |
|---|
| 69 |
|
|---|
| 70 |
case '"': |
|---|
| 71 |
if ( $is_open_tb && !$is_open_dq && !$is_open_sq ) { |
|---|
| 72 |
$is_open_dq = true; |
|---|
| 73 |
} else if ( $is_open_tb && $is_open_dq && !$is_open_sq ) { |
|---|
| 74 |
$is_open_dq = false; |
|---|
| 75 |
} else { |
|---|
| 76 |
$chr = '"'; |
|---|
| 77 |
} |
|---|
| 78 |
break; |
|---|
| 79 |
|
|---|
| 80 |
case "'": |
|---|
| 81 |
if ( $is_open_tb && !$is_open_dq && !$is_open_sq ) { |
|---|
| 82 |
$is_open_sq = true; |
|---|
| 83 |
} else if ( $is_open_tb && !$is_open_dq && $is_open_sq ) { |
|---|
| 84 |
$is_open_sq = false; |
|---|
| 85 |
} |
|---|
| 86 |
break; |
|---|
| 87 |
} |
|---|
| 88 |
$goodStr .= $chr; |
|---|
| 89 |
} |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
//now that the page is valid (I hope) for strip_tags, strip all unwanted tags |
|---|
| 93 |
|
|---|
| 94 |
$goodStr = strip_tags( $goodStr, '<title><hr><h1><h2><h3><h4><h5><h6><div><p><pre><sup><ul><ol><br><dl><dt><table><caption><tr><li><dd><th><td><a><area><img><form><input><textarea><button><select><option>' ); |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
$badStr = preg_split( "/<\/?pre[^>]*>/i", $goodStr ); |
|---|
| 99 |
|
|---|
| 100 |
for ( $x = 0; isset($badStr[$x]) && is_string( $badStr[$x] ); $x++ ) { |
|---|
| 101 |
if ( $x % 2 ) { $badStr[$x] = '<pre>'.$badStr[$x].'</pre>'; } else { |
|---|
| 102 |
$goodStr = preg_split( "/<\/?textarea[^>]*>/i", $badStr[$x] ); |
|---|
| 103 |
for ( $z = 0; isset($goodStr[$z]) && is_string( $goodStr[$z] ); $z++ ) { |
|---|
| 104 |
if ( $z % 2 ) { $goodStr[$z] = '<textarea>'.$goodStr[$z].'</textarea>'; } else { |
|---|
| 105 |
$goodStr[$z] = str_replace(' ', ' ', $goodStr[$z] ); |
|---|
| 106 |
} |
|---|
| 107 |
} |
|---|
| 108 |
$badStr[$x] = implode('',$goodStr); |
|---|
| 109 |
} |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
$goodStr = implode('',$badStr); |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
$goodStr = preg_replace( "/<option[^>]*>[^<]*/i", '', $goodStr ); |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
$goodStr = preg_replace( "/<(\/title|hr)[^>]*>/i", "\n --------------------\n", $goodStr ); |
|---|
| 121 |
|
|---|
| 122 |
$goodStr = preg_replace( "/<(h|div|p)[^>]*>/i", "\n\n", $goodStr ); |
|---|
| 123 |
|
|---|
| 124 |
$goodStr = preg_replace( "/<sup[^>]*>/i", '^', $goodStr ); |
|---|
| 125 |
|
|---|
| 126 |
$goodStr = preg_replace( "/<(ul|ol|br|dl|dt|table|caption|\/textarea|tr[^>]*>\s*<(td|th))[^>]*>/i", "\n", $goodStr ); |
|---|
| 127 |
|
|---|
| 128 |
$goodStr = preg_replace( "/<li[^>]*>/i", "\nᅵ ", $goodStr ); |
|---|
| 129 |
|
|---|
| 130 |
$goodStr = preg_replace( "/<dd[^>]*>/i", "\n\t", $goodStr ); |
|---|
| 131 |
|
|---|
| 132 |
$goodStr = preg_replace( "/<(th|td)[^>]*>/i", "\t", $goodStr ); |
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
$goodStr = preg_replace( "/<a[^>]* href=(\"((?!\"|#|javascript:)[^\"#]*)(\"|#)|'((?!'|#|javascript:)[^'#]*)('|#)|((?!'|\"|>|#|javascript:)[^#\"'> ]*))[^>]*>([^<]*)<\/a>/i", "$7 [$2$4$6]", $goodStr ); |
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
$goodStr = preg_replace( "/<img[^>]* alt=(\"([^\"]+)\"|'([^']+)'|([^\"'> ]+))[^>]*>/i", "[$2$3$4] ", $goodStr ); |
|---|
| 139 |
|
|---|
| 140 |
$goodStr = preg_replace( "/<form[^>]* action=(\"([^\"]+)\"|'([^']+)'|([^\"'> ]+))[^>]*>/i", "\n[FORM: $2$3$4] ", $goodStr ); |
|---|
| 141 |
|
|---|
| 142 |
$goodStr = preg_replace( "/<(input|textarea|button|select)[^>]*>/i", "[INPUT] ", $goodStr ); |
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
$goodStr = strip_tags( $goodStr ); |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
$goodStr = strtr( $goodStr, array_flip( get_html_translation_table( HTML_ENTITIES ) ) ); |
|---|
| 151 |
|
|---|
| 152 |
preg_replace( "/&#(\d+);/me", "chr('$1')", $goodStr ); |
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
// $goodStr = wordwrap( $goodStr ); // Moodle |
|---|
| 157 |
$goodStr = wordwrap( $goodStr, 78 ); |
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
$goodStr = preg_replace("/\r\n?|\f/", "\n", $goodStr); |
|---|
| 161 |
$goodStr = preg_replace("/\n(\s*\n){2}/", "\n\n\n", $goodStr); |
|---|
| 162 |
$goodStr = preg_replace("/[ \t]+(\n|$)/", "$1", $goodStr); |
|---|
| 163 |
$goodStr = preg_replace("/^\n*|\n*$/", '', $goodStr); |
|---|
| 164 |
|
|---|
| 165 |
return $goodStr; |
|---|
| 166 |
|
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
?> |
|---|
| 170 |
|
|---|