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 ?

4
  • 3
    What's your budget? If you want us to make all the code, you don't expect it to be free, right? Commented Feb 21, 2014 at 15:39
  • 1
    strpos() Commented Feb 21, 2014 at 15:39
  • Thanks for that, I had a good ROLMFHO moment @h2ooooooo Commented Feb 21, 2014 at 15:40
  • 1
    Why don't you just use a dropdown select? You know how people are generally lazy entering text, yet alone risking to make a mistake while typing. What if they entered 10--//@@!!!30 just cuz? You're giving yourself more work and trouble for nothing. Commented Feb 21, 2014 at 15:53

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

3

Simply use this strstr function :

$string= 'some string with - values ';
if(strstr($string, '-')){
    echo 'symbol is isset';
}

2 Comments

sergio, although you answer is correct using MonkeyZeus's solution is less memory intensive.
agree, it is just one of the way to resolve task

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.