sanitize textarea DG - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of sanitize textarea DG.php

  1. <?php
  2. function db_escape($text){
  3.     //for example mysqli_real_escape_string
  4.     return($text);
  5. }
  6.         function br2nl($text){
  7.                 return  preg_replace('/\<\/br(\s*)?\/?\>/i', "\n", $text);
  8.         }
  9.         function _toUTF8($m){
  10.                 if(function_exists('mb_convert_encoding')){
  11.                         return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES");
  12.                 }else{
  13.                         return $m[1];
  14.                 }
  15.         }
  16.         function entToUTF8($input){
  17.                 return preg_replace_callback('/(&#[0-9]+;)/', '_toUTF8', $input);
  18.         }
  19.  
  20.         $string = "  <HACKING> òàèìù !(!«´««“‘\\\\\\\\{{{{»”’«“‘»{}}\n\n{:,.lò </HACKING>  ";
  21.  
  22.     $string = trim($string);//so we are sure it is whitespace free at both ends
  23.    
  24.     //preserve newline for textarea answers
  25.     $string = str_replace("\n","[NEWLINE]", $string);
  26.    
  27.        
  28.        $out = entToUTF8(br2nl(db_escape($string)));
  29.  
  30.         //now replace the placeholder with the original newline
  31.         $out = str_replace("[NEWLINE]", "\n", $out);
  32.     
  33.        print "input=\t".$out;
  34.        print ("\noutput=\t".$out);
  35.  
  36. ?>
File Description
  • sanitize textarea DG
  • PHP Code
  • 15 Dec-2020
  • 1017 Bytes
You can Share it: