2

Intro

In search, i want to make search for age range.

if a user inserts a '-' included input like 10-30 this way i will display list of people age greater than 10 and less than 30.

My Question

$q = mysql_real_escape_string(htmlspecialchars("user input from search field"));

From here before entering to make query i want to make two conditions, One: if there's a '-' in user input or not ? if yes i want to query as according, if not the other way.

So, how can i enter into the first condition ?

Suman KC
  • 3,478
  • 4
  • 30
  • 42

2 Answers2

3

This will work:

// use strpos() because strstr() uses more resources
if(strpos("user input from search field", "-") === false)
{
    // not found provides a boolean false so you NEED the ===
}
else
{
    // found can mean "found at position 0", "found at position 19", etc...
}

strpos()

What NOT to do

if(!strpos("user input from search field", "-"))

The example above will screw you over because strpos() can return a 0 (zero) which is a valid string position just as it is a valid array position.

This is why it is absolutely mandatory to check for === false

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
3

Simply use this strstr function :

$string= 'some string with - values ';
if(strstr($string, '-')){
    echo 'symbol is isset';
}
sergio
  • 5,210
  • 7
  • 24
  • 46