Improved strip function

This commit is contained in:
pezcurrel 2022-12-29 12:40:35 +01:00
parent 184875ada5
commit b21b4f2ec2

View file

@ -1,5 +1,7 @@
<?php
define('RN',"\r\n");
$bt=microtime(true);
$dlanguc=strtoupper($dlang);
@ -203,19 +205,22 @@ function nullyp($str) {
}
function strip($str,$uri) {
if (nullemp($str)) return(null);
while (preg_match('#^\s*<br\s*[\\\/]?>#',$str)===1) $str=preg_replace('#^\s*<br\s*[\\\/]?>#','',$str);
while (preg_match('#<br\s*[\\\/]?>\s*$#',$str)===1) $str=preg_replace('#<br\s*[\\\/]?>\s*$#','',$str);
while (preg_match('#<br\s*[\\\/]?>\s*<br\s*[\\\/]?>#s',$str)===1)
$str=preg_replace('#<br\s*[\\\/]?>\s*<br\s*[\\\/]?>#s','<br>',$str);
$str=preg_replace('#<style( [^>]*)?>.*</style>#s','',$str);
$str=preg_replace('#<a href="(?![a-zA-Z]+://)([^"]+)#','<a href="https://'.$uri.'$1>',$str);
$str=preg_replace('#<h[1-9][^>]*>#','<p><strong>',$str);
$str=preg_replace('#</h[1-9]>#','</strong></p>',$str);
$str=preg_replace(array('#</p><br>#','#</li><br>#','#</ul><br>#','#<ul><br>#'),array('</p>','</li>','</ul>','<ul>'),$str);
$str=preg_replace('#<b>#','<strong>',$str);
$str=preg_replace('#</b>#','</strong>',$str);
$str=preg_replace('#<p></p>#','',$str);
while (preg_match('#^\s*<br\s*[\\\/]?>#i',$str)===1) $str=preg_replace('#^\s*<br\s*[\\\/]?>#i','',$str);
while (preg_match('#<br\s*[\\\/]?>\s*$#i',$str)===1) $str=preg_replace('#<br\s*[\\\/]?>\s*$#i','',$str);
while (preg_match('#<br\s*[\\\/]?>\s*<br\s*[\\\/]?>#si',$str)===1) $str=preg_replace('#<br\s*[\\\/]?>\s*<br\s*[\\\/]?>#si','<br>',$str);
$str=preg_replace('#<style( [^>]*)?>.*</style>#si','',$str);
$str=preg_replace('#<a href="(?![a-zA-Z]+://)([^"]+)#i','<a href="https://'.$uri.'$1>',$str);
$str=preg_replace('#<h[1-9][^>]*>#i','<p class="exh">',$str);
$str=preg_replace('#</h[1-9]>#i','</p>',$str);
$str=preg_replace(['#</p><br>#i','#</li><br>#i','#</ul><br>#i','#<ul><br>#i'],['</p>','</li>','</ul>','<ul>'],$str);
$str=preg_replace('#<b>#i','<strong>',$str);
$str=preg_replace('#</b>#i','</strong>',$str);
$str=preg_replace('#<p></p>#i','',$str);
$str=strip_tags($str,'<a><br><ol><ul><li><p><div><i><b><strong><em><small><img>');
$str=preg_replace('#[\r\n]#m','',$str);
$str=preg_replace('#(<p[^>]*>|<ol[^>]*>|<ul[^>]*>|<div[^>]*>)#i',N.'$1',$str);
$str=preg_replace('#(</p>|</ol>|</ul>|</div>)#i','$1'.N,$str);
$str=preg_replace(['#<p[^>]*>(.*)</p>$#i', '#<ol[^>]*>(.*)</ol>$#i', '#<ul[^>]*>(.*)</ul>$#i', '#<div[^>]*>(.*)</div>$#i'],['<p class="nobott">$1</p>', '<ol class="nobott">$1</ol>', '<ul class="nobott">$1</ul>', '<div class="nobott">$1</div>'],$str);
return($str);
}
@ -584,7 +589,7 @@ while ($if<$il && $row=mysqli_fetch_assoc($res)) {
if (mysqli_num_rows($rres)>0) {
$out.='<ol class="nobott">'.N;
while ($rrow=mysqli_fetch_assoc($rres))
$out.='<li>'.hspech($rrow['Text']).'</li>'.N;
$out.='<li>'.hspech($rrow['Text']).'</li>'.N;// no strip, do hspech, because it doesnt support html nor markdown in mastodon
$out.='</ol>'.N;
} else {
$out.='<span class="null">'._('Not available{plural}').'</span>';
@ -598,7 +603,7 @@ while ($if<$il && $row=mysqli_fetch_assoc($res)) {
$out.='<ul class="nobott">'.N;
while ($rrow=mysqli_fetch_assoc($rres)) {
$out.='<li><strong>'.hspech($rrow['Domain']).'</strong>: '.$sevmap[$rrow['Severity']];
if (!is_null($rrow['Comment'])) $out.=' - '.hspech($rrow['Comment']);
if (!is_null($rrow['Comment'])) $out.=' - '.hspech($rrow['Comment']);// no strip, do hspech, because it doesnt support html nor markdown in mastodon
$out.='</li>'.N;
}
$out.='</ul>'.N;