Skip to content

Instantly share code, notes, and snippets.

@anjerodesu
Last active December 10, 2015 22:29
Show Gist options
  • Save anjerodesu/4502922 to your computer and use it in GitHub Desktop.
Save anjerodesu/4502922 to your computer and use it in GitHub Desktop.
<?php
/********************************************************************************************/
/* HTMLSpecialEntity created by Angelo Villegas [http://www.studiovillegas.com] */
/* */
/* Copyright © Angelo Villegas */
/* Permission is hereby granted, free of charge, to any person obtaining a copy of this */
/* software and associated documentation files (the "Software"), to deal in the Software */
/* without restriction, including without limitation the rights to use, copy, modify, */
/* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to the following */
/* conditions: */
/* */
/* The above copyright notice and this permission notice shall be included in all copies */
/* or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, */
/* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A */
/* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF */
/* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE */
/* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/********************************************************************************************/
/********************************************************************************/
/* Dellimiter Instructions */
/* HSE_DEFAULT = Will convert single quotes. */
/* HSE_DOUBLEQUOTES = Will convert double-quotes and leave single-quotes alone. */
/* HSE_QUOTES = Will convert both double and single quotes. */
/* HSE_REVERSQUOTES = Reverse of the ENT_QUOTES conversion. */
/* HSE_REVERSEDOUBLE = Reverse of the ENT_DOUBLEQUOTES conversion. */
/* HSE_REVERSESINGLE = Revers of the ENT_DEFAULT conversion. */
/********************************************************************************/
/********************************************************************************/
/* Special Convert Instructions */
/* HSE_REVERSE = Will convert special characters from $key to $value */
/* HSE_TRUE = Will convert special characters from $value to $key */
/* HSE_FALSE = Will do nothing */
/********************************************************************************/
class HTMLSpecialEntity
{
function removeNBSP( $string )
{
$string = str_replace( '&nbsp;' , ' ' , $string );
return $string;
}
function specialChars( $string , $dellimeter = HSE_DEFAULT , $specialConvert = HSE_FALSE )
{
$html_entities = array (
'&amp;' => '&' , '&quot;' => '"' ,
/*'&lt;' => '<' , '&gt;' => '>' ,*/
'&#8211;' => '–' , '&#8212;' => '—' ,
'&Aacute;' => 'Á' , '&aacute;' => 'á' ,
'&Acirc;' => 'Â' , '&acirc;' => 'â' ,
'&Agrave;' => 'À' , '&agrave;' => 'à' ,
'&Aring;' => 'Å' , '&aring;' => 'å' ,
'&Atilde;' => 'Ã' , '&atilde;' => 'ã' ,
'&Auml;' => 'Ä' , '&auml;' => 'ä' ,
'&AElig;' => 'Æ' , '&aelig;' => 'æ' ,
'&Ccedil;' => 'Ç' , '&ccedil;' => 'ç' ,
'&eacute;' => 'é' , '&Eacute;' => 'É' ,
'&Ecirc;' => 'Ê' , '&ecirc;' => 'ê' ,
'&egrave;' => 'è' , '&Egrave;' => 'È' ,
'&ETH;' => 'Ð' , '&eth;' => 'ð' ,
'&Euml;' => 'Ë' , '&euml;' => 'ë' ,
'&Iacute;' => 'Í' , '&iacute;' => 'í' ,
'&Icirc;' => 'Î' , '&icirc;' => 'î' ,
'&Igrave;' => 'Ì' , '&igrave;' => 'ì' ,
'&Iuml;' => 'Ï' , '&iuml;' => 'ï' ,
'&Ntilde;' => 'Ñ' , '&ntilde;' => 'ñ' ,
'&Oacute;' => 'Ó' , '&oacute;' => 'ó' ,
'&Ocirc;' => 'Ô' , '&ocirc;' => 'ô' ,
'&ograve;' => 'ò' , '&Ograve;' => 'Ò' ,
'&Oslash;' => 'Ø' , '&oslash;' => 'ø' ,
'&otilde;' => 'õ' , '&Otilde;' => 'Õ' ,
'&ouml;' => 'ö' , '&Ouml;' => 'Ö' ,
'&SZlig;' => 'ẞ' , '&szlig;' => 'ß' ,
'&Thorn;' => 'Þ' , '&thorn;' => 'þ' ,
'&uacute;' => 'ú' , '&Uacute;' => 'Ú' ,
'&ucirc;' => 'û' , '&Ucirc;' => 'Û' ,
'&Ugrave;' => 'Ù' , '&ugrave;' => 'ù' ,
'&uuml;' => 'ü' , '&Uuml;' => 'Ü' ,
'&yacute;' => 'ý' , '&Yacute;' => 'Ý' ,
'&Yuml;' => 'Ÿ' , '&yuml;' => 'ÿ'
);
switch ( $dellimeter )
{
case HSE_DEFAULT :
break;
case HSE_QUOTES :
$string = str_replace( "'" , "\'" , $string );
$string = str_replace( '"' , '\"' , $string );
break;
case HSE_REVERSEQUOTES :
$string = str_replace( "\'" , "'" , $string );
$string = str_replace( '\"' , '"' , $string );
break;
case HSE_SINGLEQUOTES :
$string = str_replace( "'" , "\'" , $string );
break;
case HSE_DOUBLEQUOTES :
$string = str_replace( '"' , '\"' , $string );
break;
case HSE_REVERSEDOUBLE :
$string = str_replace( '\"' , '"' , $string );
break;
case HSE_REVERSESINGLE :
$string = str_replace( "\'" , "'" , $string );
break;
}
if( $specialConvert === HSE_TRUE )
{
foreach( $html_entities as $key => $value )
{
$string = str_replace( $value , $key , $string );
}
}
elseif( $specialConvert === HSE_REVERSE )
{
foreach( $html_entities as $key => $value )
{
$string = str_replace( $key , $value , $string );
}
}
$string = trim( $string );
return $string;
}
function trimAll( $string )
{
$string = str_replace( "\r\n" , '<br />' , $string );
$string = str_replace( "\n\r" , '<br />' , $string );
$string = str_replace( "\r" , '<br />' , $string );
$string = str_replace( "\n" , '<br />' , $string );
$string = str_replace( "\0" , '<br />' , $string );
$string = str_replace( "\x0B" , '<br />' , $string );
$string = trim( $string );
return $string;
}
function replaceChars( $string , $reverse = false )
{
$special = array(
'À' , 'Á' , 'Â' , 'Ã' , 'Ä' , 'Å' , 'Æ' , 'Ç' , 'È' , 'É' , 'Ê' , 'Ë' ,
'Ì' , 'Í' , 'Î' , 'Ï' , 'Ð' , 'Ñ' , 'Ò'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' , 'Ů' , 'ů' ,
'Ű' , 'ű' , 'Ų' , 'ų' , 'Ŵ' , 'ŵ' , 'Ŷ' , 'ŷ' , 'Ÿ' , 'Ź' , 'ź' , 'Ż' ,
'ż' , 'Ž' , 'ž' , 'ſ' , 'ƒ' , 'Ơ' , 'ơ' , 'Ư' , 'ư' , 'Ǎ' , 'ǎ' , 'Ǐ' ,
'ǐ' , 'Ǒ' , 'ǒ' , 'Ǔ' , 'ǔ' , 'Ǖ' , 'ǖ' , 'Ǘ' , 'ǘ' , 'Ǚ' , 'ǚ' , 'Ǜ' ,
'ǜ' , 'Ǻ' , 'ǻ' , 'Ǽ' , 'ǽ' , 'Ǿ' , 'ǿ' , '‘' , '’' , '‚' , '“' , '”' ,
'„' , '—' , '–' , '¯' , '¢' , '£' , '¤' , '¥' , '¦' , '§' , '¨' , '°' ,
'©' , '®'
);
$normal = array(
'A' , 'A' , 'A' , 'A' , 'A' , 'A' , 'AE' , 'C' , 'E' , 'E' , 'E' , 'E' ,
'I' , 'I' , 'I' , 'I' , 'D' , 'N' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' ,
'U' , 'U' , 'U' , 'U' , 'Y' , 's' , 'a' , 'a' , 'a' , 'a' , 'a' , 'a' ,
'ae' , 'c' , 'e' , 'e' , 'e' , 'e' , 'i' , 'i' , 'i' , 'i' , 'n' , 'o' ,
'o' , 'o' , 'o' , 'o' , 'o' , 'u' , 'u' , 'u' , 'u' , 'y' , 'y' , 'A' ,
'a' , 'A' , 'a' , 'A' , 'a' , 'C' , 'c' , 'C' , 'c' , 'C' , 'c' , 'C' ,
'c' , 'D' , 'd' , 'D' , 'd' , 'E' , 'e' , 'E' , 'e' , 'E' , 'e' , 'E' ,
'e' , 'E' , 'e' , 'G' , 'g' , 'G' , 'g' , 'G' , 'g' , 'G' , 'g' , 'H' ,
'h' , 'H' , 'h' , 'I' , 'i' , 'I' , 'i' , 'I' , 'i' , 'I' , 'i' , 'I' ,
'i' , 'IJ' , 'ij' , 'J' , 'j' , 'K' , 'k' , 'L' , 'l' , 'L' , 'l' , 'L' ,
'l' , 'L' , 'l' , 'l' , 'l' , 'N' , 'n' , 'N' , 'n' , 'N' , 'n' , 'n' ,
'O' , 'o' , 'O' , 'o' , 'O' , 'o' , 'OE' , 'oe' , 'R' , 'r' , 'R' , 'r' ,
'R' , 'r' , 'S' , 's' , 'S' , 's' , 'S' , 's' , 'S' , 's' , 'T' , 't' ,
'T' , 't' , 'T' , 't' , 'U' , 'u' , 'U' , 'u' , 'U' , 'u' , 'U' , 'u' ,
'U' , 'u' , 'U' , 'u' , 'W' , 'w' , 'Y' , 'y' , 'Y' , 'Z' , 'z' , 'Z' ,
'z' , 'Z' , 'z' , 's' , 'f' , 'O' , 'o' , 'U' , 'u' , 'A' , 'a' , 'I' ,
'i' , 'O' , 'o' , 'U' , 'u' , 'U' , 'u' , 'U' , 'u' , 'U' , 'u' , 'U' ,
'u' , 'A' , 'a' , 'AE' , 'ae' , 'O' , 'o' , '\'' , '\'' , ',' , '"' , '"' ,
',,' , '-' , '-' , '-' , 'c' , 'L' , 'o' , 'Y' , 'l' , 'S' , '-' , 'o' ,
'R' , 'C'
);
$string = str_replace( "\'" , "'" , $string );
$string = str_replace( '\"' , '"' , $string );
$string = str_replace( "\r\n" , '' , $string );
$string = str_replace( "\n\r" , '' , $string );
$string = str_replace( "\r" , '' , $string );
$string = str_replace( "\n" , '' , $string );
$string = str_replace( "\0" , '' , $string );
$string = str_replace( "\x0B" , '' , $string );
if ( $reverse === true )
{
$string = str_replace( $normal , $special , $string );
}
else
{
$string = str_replace( $special , $normal , $string );
}
return $string;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment