- var_dump: documentation ( source)
- base_convert: documentation ( source)
- mb_substr: documentation ( source)
- mb_strlen: documentation ( source)
- preg_match: documentation ( source)
- in_array: documentation ( source)
- ord: documentation ( source)
<?php
function getUTF8CharCode($c) {
$h = ord($c{0});
if ($h <= 0x7F) {
return $h;
} else if ($h < 0xC2) {
return false;
} else if ($h <= 0xDF) {
return ($h & 0x1F) << 6 | (ord($c{1}) & 0x3F);
} else if ($h <= 0xEF) {
return ($h & 0x0F) << 12 | (ord($c{1}) & 0x3F) << 6
| (ord($c{2}) & 0x3F);
} else if ($h <= 0xF4) {
return ($h & 0x0F) << 18 | (ord($c{1}) & 0x3F) << 12
| (ord($c{2}) & 0x3F) << 6
| (ord($c{3}) & 0x3F);
} else {
return false;
}
}
function escapeCSSCharacter($c) {
return "\\" . base_convert(getUTF8CharCode($c), 10, 16) . " ";
}
function escapeCSSRule($data, array $immuneChars = array()) {
$result = "";
for ($i = 0; $i < mb_strlen($data); $i++) {
$currChar = mb_substr($data, $i, 1);
if (getUTF8CharCode($currChar) > 256 && !preg_match("/^\w$/", $currChar) && !in_array($currChar, $immuneChars)) {
var_dump(getUTF8CharCode($currChar), preg_match("/^\w$/", $currChar));
$result .= escapeCSSCharacter($currChar);
}
else {
$result .= $currChar;
}
}
return $result;
}
echo escapeCSSRule("#BADA55");