-2

I have created a search form and it's only able to search when user input is exactly what's in the database. For example, if "hello" is in the database, then the search succeeds, but if the user were to add white space (e.g. " hello "), the search fails. What can I do to ignore the white space in php?

peak
  • 105,803
  • 17
  • 152
  • 177
whatever
  • 7
  • 1
  • 7
  • If you're just doing a search in PHP rather than a SQL query, just use strpos and see if it's false or not. In MySQL, you can use the `LIKE` operator to search for a string inside a larger string. – Matt Jan 12 '16 at 03:05
  • Can you include your code you have written that currently performs the search? There's many methods of implementing a search form, so it's hard to say how to modify your specific approach. – ajshort Jan 12 '16 at 03:11

1 Answers1

3

Use the trim function to remove unwanted white space:

$searchString = trim($searchString);

Read more here: http://php.net/trim

trim — Strip whitespace (or other characters) from the beginning and end of a string

Usage: string trim ( string $str [, string $character_mask = " \t\n\r\0\x0B" ] )

Community
  • 1
  • 1
Clay
  • 4,700
  • 3
  • 33
  • 49