Sabtu, 31 Maret 2012

Broken Tag Detector (PHP)

function broken_tags($str)
{
preg_match_all("/(<\w+)(?:.){0,}?>/", $str, $v1);
preg_match_all("/<\/\w+>/", $str, $v2);
$open = array_map('strtolower', $v1[1]);
$closed = array_map('strtolower', $v2[0]);
foreach ($open as $tag)
{
$end_tag = preg_replace("/<(.*)/", "</$1>", $tag);
if (!in_array($end_tag, $closed)) return true;
unset($closed[array_search($end_tag, $closed)]);
}
return false;
}

Element Event Remover (PHP)

function remove_event($sSource, $aDisabledAttributes=false){
if(!is_array($aDisabledAttributes)){
$aDisabledAttributes = array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavaible', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragdrop', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterupdate', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmoveout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload');
}

$sSource = stripcslashes($sSource);
if(empty($aDisabledAttributes)){
return $sSource;
}

$aDisabledAttributes = @implode('|', $aDisabledAttributes);
$sSource = preg_replace('/<(.*?)>/ie', "'<' . preg_replace(array('/javascript:[^\"\']*/i', '/(".$aDisabledAttributes.")[ \\t\\n]*=[ \\t\\n]*[\"\'][^\"\']*[\"\']/i', '/\s+/'), array('', '', ' '), stripslashes('\\1')) . '>'", $sSource );
//$sSource = preg_replace('/\s(' . $aDisabledAttributes . ').*?([\s\>])/', '\\2', $sSource);
//$sSource = preg_replace('@[a-z]*=""@is', '', $sSource);
return $sSource;
}

UTF8 to Entity Converter (PHP)

function UTF8ToEntities($string){
if (!ereg("[\200-\237]",$string) && !ereg("[\241-\377]",$string))
    return $string;
$string = preg_replace("/[\302-\375]([\001-\177])/","&#65533;\\1",$string);
$string = preg_replace("/[\340-\375].([\001-\177])/","&#65533;\\1",$string);
$string = preg_replace("/[\360-\375]..([\001-\177])/","&#65533;\\1",$string);
$string = preg_replace("/[\370-\375]...([\001-\177])/","&#65533;\\1",$string);
$string = preg_replace("/[\374-\375]....([\001-\177])/","&#65533;\\1",$string);
$string = preg_replace("/[\300-\301]./", "&#65533;", $string);
$string = preg_replace("/\364[\220-\277]../","&#65533;",$string);
$string = preg_replace("/[\365-\367].../","&#65533;",$string);
$string = preg_replace("/[\370-\373]..../","&#65533;",$string);
$string = preg_replace("/[\374-\375]...../","&#65533;",$string);
$string = preg_replace("/[\376-\377]/","&#65533;",$string);
$string = preg_replace("/[\302-\364]{2,}/","&#65533;",$string);
$string = preg_replace(
    "/([\360-\364])([\200-\277])([\200-\277])([\200-\277])/e",
    "'&#'.((ord('\\1')&7)<<18 | (ord('\\2')&63)<<12 |".
    " (ord('\\3')&63)<<6 | (ord('\\4')&63)).';'",
$string);
$string = preg_replace("/([\340-\357])([\200-\277])([\200-\277])/e",
"'&#'.((ord('\\1')&15)<<12 | (ord('\\2')&63)<<6 | (ord('\\3')&63)).';'",
$string);
$string = preg_replace("/([\300-\337])([\200-\277])/e",
"'&#'.((ord('\\1')&31)<<6 | (ord('\\2')&63)).';'",
$string);
$string = preg_replace("/[\200-\277]/","&#65533;",$string);
return $string;
}

Read and Write Cookie (JavaScript)

// Example:
// writeCookie("myCookie", "my name", 24);
// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.
function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
}
// Example:
// alert( readCookie("myCookie") );
function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  {
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    {
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

UTF8 to Entity Converter (JavaScript)

function utf8ToEntity(utf8) {
String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,'');}
var utf8_char = utf8;
utf8_char=utf8_char.trim();
var html_entity='';
for(i=0; i<=utf8_char.length; i++){
if(utf8_char.charCodeAt(i)>127){html_entity+='&#'+utf8_char.charCodeAt(i) + ';';}
else{html_entity += utf8_char.charAt(i);}
}
return html_entity;
}

HTML Entity Encoder-Decoder (JavaScript)

function htmlEntityEncode(data){
var output=data;
output=replaceStr('"','&quot;',output,true);
output=replaceStr('&','&amp;',output,true);
output=replaceStr('<','&lt;',output,true);
output=replaceStr('>','&gt;',output,true);
return output;
}
function htmlEntityDecode(data){
var output=data;
output=replaceStr('&gt;','>',output,true);
output=replaceStr('&lt;','<',output,true);
output=replaceStr('&amp;','&',output,true);
output=replaceStr('&quot;','"',output,true);
return output;
}

in_array Function (JavaScript)

function in_array(needle,haystack){
var i;
for(i in haystack){
if(needle==haystack[i]){return true;}
}
return false;
}