note 100529 added to function.htmlentities

From: Date: Thu, 21 Oct 2010 20:23:09 +0000
Subject: note 100529 added to function.htmlentities
Groups: php.notes 
Request: Send a blank email to php-notes+get-173347@lists.php.net to get a copy of this message
Thanks to all those who's comments, concerns and contributions went into this! You will need the arrays I posted on Oct 12, 2010: $HTML_ENTS, $HTML_TAGS, $Xchars <?php /* toXHTML.php see: http://softmoon-webware.com/php_functions-instructions.php#toXHTML // this will encode all "stray" (X)HTML entites as needed // without effecting "valid" existing markup. // it will also apply nl2br "intelligently", // and handle multiple spaces to your liking: /*note the alternative format for passing in options*/ Function toXHTML( $S, // array(userOpt=>value, ...) ) $nl_to_br=TRUE, $allowJS=TRUE, $collapse_spaces='toXHTML', $xhtml=TRUE, $retain_formatting=TRUE, $strip_CtrlChrs=TRUE, $charset='ISO-8859-1', $tabspacing=8, $hardspace='&nbsp;', $skip_Quotes=FALSE ) { if (func_num_args()==2 and is_array($nl_to_br)) { $userArgs=$nl_to_br; $nl_to_br=TRUE; $userOpts=array('nl_to_br', 'allowJS', 'collapse_spaces', 'xhtml', 'retain_formatting', 'strip_CtrlChrs', 'charset', 'tabspacing', 'hardspace', 'skip_Quotes'); foreach ($userArgs as $var => $value) { if (in_array($var, $userOpts)) $$var=$value; } } $sl=strlen($S); $build=""; $SPflag=FALSE; $PREflag=TRUE; $inQuotes=FALSE; $line_term=(strpos($S, chr(10))!==FALSE) ? 10 : 13; $tagRegEx=($xhtml) ? '/^<\/?([a-z]{1,10})([ \t\n\r]+[a-z]{2,12}=[ \t\n\r]*' .'("[^\x00-\x1F"<>]*"|\'[^\x00-\x1F\'<>]*\'))*( \/)?>/' : '/^<\/?([a-z]{1,10})([ \t\n\r]+[^<>]*)?>/i'; for ($i=0; $i<$sl; $i++) { $cc=ord($sS=substr($S, $i)); if ($nl_to_br and $cc==0x0D and $PREflag) { $build.=(($xhtml) ? "<br />" : "<br>"); $SPflag=FALSE; } if ($collapse_spaces!==TRUE and $cc==0x09 and $tabspacing and $PREflag and ($inQuotes or !$skip_Quotes)) { $build.=( ($collapse_spaces==='toXHTML') ? str_repeat((($SPflag) ? "$hardspace " : " $hardspace"), $tabspacing/2) : str_repeat(" ", $tabspacing) ); continue; } if ($PREflag and $strip_CtrlChrs and $cc<0x20 and !($retain_formatting and ($cc==0x09 or $cc==0x0D))) continue; if ($cc==0x20 and $collapse_spaces and $SPflag and $PREflag) { if ($collapse_spaces==='toXHTML') { if ($inQuotes or !$skip_Quotes) {$build.=$hardspace; $SPflag=FALSE;} else $build.=" "; } continue; } if ($SPflag=($cc==0x20 or $cc==$line_term )) { $build.=chr($cc); continue; } if ($skip_Quotes and ($cc==0x22 or $cc==0x27)) { $build.=chr($cc); $inQuotes=($inQuotes===$cc) ? FALSE : (($inQuotes===FALSE) ? $cc : $inQuotes); continue; } if ($xhtml and $cc>0x7F and $cc<0xA0) { $build.=($Xchars[$cc]) ? $Xchars[$cc] : " "; continue; } if (preg_match('/^&(#?[a-z0-9]{2,8});/i', $sS, $susp) and (in_array($susp[1], $HTML_ENTS) or preg_match('/^#(x[0-9a-f]{2,4}|[0-9]{2,4})$/i', $susp[1]))) { $build.=$susp[0]; $i+=strlen($susp[0])-1; $SPflag=FALSE; continue; } if (preg_match($tagRegEx, $sS, $susp) and in_array($susp[1], $HTML_TAGS)) { $sS=substr($susp[0], 1, strlen($susp[0])-2); $build.="<" .toXHTML(($allowJS) ? $sS : preg_replace( '/on(click|dblclick|keydown|keypress|keyup|' .'mousedown|mousemove|mouseout|' .'mouseover|mouseup|focus|blur)[ \t\n\r]*=[ \t\n\r]*([\'"]).*\2/i', "", preg_replace( '/(href|src)[ \t\n\r]*=[ \t\n\r]*([\'"])javascript:.*\2/i', "$1=''", $sS)), FALSE, $allowJS, $collapse_spaces, $xhtml, $retain_formatting, $strip_CtrlChrs, $charset, $tabspacing, $hardspace, TRUE) .">"; $i+=strlen($susp[0])-1; $SPflag=FALSE; if ( ($x=strpos($sS, 'pre')) !==FALSE and $x<2) $PREflag=(boolean)$x; continue; } $build.=htmlentities(substr($sS, 0, 1), ENT_QUOTES, $charset); } return $build; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 74.160.1.30 ---- Manual Page -- http://www.php.net/manual/en/function.htmlentities.php Edit -- https://master.php.net/note/edit/100529 Del: integrated -- https://master.php.net/note/delete/100529/integrated Del: useless -- https://master.php.net/note/delete/100529/useless Del: bad code -- https://master.php.net/note/delete/100529/bad+code Del: spam -- https://master.php.net/note/delete/100529/spam Del: non-english -- https://master.php.net/note/delete/100529/non-english Del: in docs -- https://master.php.net/note/delete/100529/in+docs Del: other reasons-- https://master.php.net/note/delete/100529 Reject -- https://master.php.net/note/reject/100529 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#173347) next »