note 100389 added to function.htmlentities

From: Date: Wed, 13 Oct 2010 03:13:44 +0000
Subject: note 100389 added to function.htmlentities
Groups: php.notes 
Request: Send a blank email to php-notes+get-173177@lists.php.net to get a copy of this message
Special thanks to all those whose comments, concerns, and contributions went into this code, as well as all those on this site. You will need the arrays I posted in my earlier note: $HTML_ENTS, $HTML_TAGS, and $Xchars <?php /* encode to XHTML for instructions, fully commented sourcecode, and test example, see: http://softmoon-webware.com/php_functions-instructions.php/ http://softmoon-webware.com/php_functions.zip/ // this will encode all 'stray' (X)HTML entites as needed // without effecting 'valid' existing markup. It will also // apply nl2br 'intelegently', and format spaces to your liking. /*note this alternative format for passing in options*/ Function XHTMLencode( $S, // array(userOpt=>value, ...) ) $nl_to_br=TRUE, $allowJS=TRUE, $collapse_spaces='toXHTML', $xhtml=TRUE, $strip_formatting=TRUE, $charset='ISO-8859-1', $tabspacing=2, $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", "strip_formatting", "charset", "tabspacing", "hardspace", "skip_DQuotes"); 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 ($strip_formatting and $cc<0x20 and $PREflag) 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.="<" .XHTMLencode(($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, $strip_formatting, $charset, $tabspacing, $hardspace, TRUE) .">"; $i+=strlen($susp[0])-1; $SPflag=FALSE; if ($nl_to_br and ($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.133.170 ---- Manual Page -- http://www.php.net/manual/en/function.htmlentities.php Edit -- https://master.php.net/note/edit/100389 Del: integrated -- https://master.php.net/note/delete/100389/integrated Del: useless -- https://master.php.net/note/delete/100389/useless Del: bad code -- https://master.php.net/note/delete/100389/bad+code Del: spam -- https://master.php.net/note/delete/100389/spam Del: non-english -- https://master.php.net/note/delete/100389/non-english Del: in docs -- https://master.php.net/note/delete/100389/in+docs Del: other reasons-- https://master.php.net/note/delete/100389 Reject -- https://master.php.net/note/reject/100389 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#173177) next »