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. //create an array of all relevant textareas
  20. $textareas = array("ta1");
  21.  
  22. foreach($_POST as $k => $v)
  23.     {
  24.         $v = trim($v);//so we are sure it is whitespace free at both ends
  25.    
  26.         //preserve newline for textarea answers
  27.         if(in_array($k,$textareas))$v=str_replace("\n","[NEWLINE]",$v);
  28.    
  29.         //sanitise string
  30.         $v = filter_var($v, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_BACKTICK);
  31.    
  32.         //now replace the placeholder with the original newline
  33.         $_POST[$k] = str_replace("[NEWLINE]","\n",$v);
  34.     }      
  35.         $string="<HACKING> òàèìù !(!«´««“‘\\\\\\\\{{{{»”’«“‘»{}}\n\n{:,.lò </HACKING>";
  36.         $_POST['ta1']=$string;
  37.         $_POST['txt1']=$string;
  38.        
  39. //     $out = entToUTF8(br2nl(db_escape($string)));
  40. //     print "input=\t".$out;
  41. //     print ("\noutput=\t".$out);
  42.  
  43. ?>
  44. <!doctype html>
  45. <html>
  46. <head>
  47. <meta charset="utf-8">
  48. <title>Filter test</title>
  49. </head>
  50.  
  51. <body>
  52.    
  53. <form action="" method="post">
  54.     <p>
  55.         <textarea name="ta1" cols="30" rows="10"><?php echo $_POST['ta1']; ?></textarea>
  56.     </p>
  57.     <p>
  58.         <input type="text" name="txt1" size="30" value="<?php echo $_POST['txt1']; ?>" />
  59.     </p>
  60.     <p>
  61.         <input type="submit" />   
  62.     </p>
  63.     </form>
  64.  
  65. </body>
  66.    
  67. </html>
File Description
  • sanitize textarea DG
  • PHP Code
  • 15 Dec-2020
  • 1.7 Kb
You can Share it: