开发者

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:

  1. " " (ASCII 32 (0x20)), an ordinary space.
  2. "\t" (ASCII 9 (0x09)), a tab.
  3. "\n" (ASCII 10 (0x0A)), a new line (line feed).
  4. "\r" (ASCII 13 (0x0D)), a carriage return.
  5. "\0" (ASCII 0 (0x00)), the NUL-byte.
  6. "\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']));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜