javascript htmlspecialchars code example

Example 1: htmlspecialchars

<?php
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
?>

Example 2: htmlspecialchars for jsp

StringBuffer sb = new StringBuffer();

for(int i=0; i<content.length(); i++){

char c = content.charAt(i);

  switch (c) {

    case '<' : 

      sb.append("&lt;");

      break;

    case '>' : 

      sb.append("&gt;");

      break;

    case '&' :

      sb.append("&amp;");

      break;

    case '"' :

      sb.append("&quot;");

      break;

    case '\'' :

      sb.append("&apos;");

      break;

    default:

      sb.append(c);

	}

}

Tags:

Php Example