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.         $string="placeholder";
  37.         if(!isset($_POST)){
  38.             $_POST['ta1']=$string;
  39.             $_POST['txt1']=$string;
  40.         }
  41. //     $out = entToUTF8(br2nl(db_escape($string)));
  42. //     print "input=\t".$out;
  43. //     print ("\noutput=\t".$out);
  44.  
  45. ?>
  46. <!doctype html>
  47. <html>
  48. <head>
  49. <meta charset="utf-8">
  50. <title>Filter test</title>
  51. </head>
  52.  
  53. <body>
  54.    
  55. <form action="" method="post">
  56.     <p>
  57.         <textarea name="ta1" cols="30" rows="10"><?php echo $_POST['ta1']; ?></textarea>
  58.     </p>
  59.     <p>
  60.         <input type="text" name="txt1" size="30" value="<?php echo $_POST['txt1']; ?>" />
  61.     </p>
  62.     <p>
  63.         <input type="submit" />   
  64.     </p>
  65.     </form>
  66.  
  67. </body>
  68.    
  69. </html>
File Description
  • sanitize textarea DG
  • PHP Code
  • 15 Dec-2020
  • 1.75 Kb
You can Share it: