Trim function not really trimming
I am using php; and i read that if i am asking the user to enter some information about himself through a form then when i access that information;i should better use
$var=mysqli_real_escape_string($dbc,trim($_POST['name']));
as it will delete all the spaces from the text fi开发者_StackOverflow社区eld and other unwanted symbols, but it really is not deleting the spaces? Help!
trim()
does not delete ALL spaces, only leading and trailing spaces.
For Just Spaces, use $string = str_replace(' ', '', $string); to remove all spaces of string
For white spaces use $string = preg_replace('/\s+/', '', $string);
trim()
will remove from the start and end of the string, the following:
- " " (ASCII 32 (0x20)), an ordinary space.
- "\t" (ASCII 9 (0x09)), a tab.
- "\n" (ASCII 10 (0x0A)), a new line (line feed).
- "\r" (ASCII 13 (0x0D)), a carriage return.
- "\0" (ASCII 0 (0x00)), the NUL-byte.
- "\x0B" (ASCII 11 (0x0B)), a vertical tab.
In addition, as also noted by @Wesley Murch, in the comments, trim will remove any character you pass to the second argument of the function, too. E.g.
$str = 'appHello Worldapp';
$clean_str = trim($str, 'ap'); // as noted by @venimus, use just a single char,
// which you want removed
echo $clean_str; // output: Hello World
try:
$var=mysqli_real_escape_string($dbc,str_replace(" ", "", $_POST['name']));
精彩评论