Created
October 27, 2013 23:39
-
-
Save divinity76/7189255 to your computer and use it in GitHub Desktop.
PHP css_escape_string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//CSS escape code ripped from Zend Framework ( https://github.com/zendframework/zf2/blob/master/library/Zend/Escaper/Escaper.php ) | |
function css_escape_string($string) | |
{ | |
$cssMatcher=function($matches) | |
{ | |
$chr = $matches[0]; | |
if (strlen($chr) == 1) { | |
$ord = ord($chr); | |
} else { | |
$chr = mb_convert_encoding($chr,'UTF-16BE','UTF-8');//$this->convertEncoding($chr, 'UTF-16BE', 'UTF-8'); | |
$ord = hexdec(bin2hex($chr)); | |
} | |
return sprintf('\\%X ', $ord); | |
}; | |
$originalEncoding=mb_detect_encoding($string); | |
if($originalEncoding===false){$originalEncoding='UTF-8';}; | |
$string = mb_convert_encoding($string,'UTF-8',$originalEncoding);//$this->toUtf8($string); | |
// throw new Exception('mb_convert_encoding(\''.$string.'\',\'UTF-8\',\''.$originalEncoding.'\');'); | |
if ($string === '' || ctype_digit($string)) { | |
return $string; | |
} | |
$result = preg_replace_callback('/[^a-z0-9]/iSu', /*$this->*/$cssMatcher, $string); | |
//var_dump($result); | |
return mb_convert_encoding($result,$originalEncoding,'UTF-8');//$this->fromUtf8($result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment