Page 1 of 1

Need function like strstr

Posted: Mon May 28, 2007 1:10 am
by boss01
I have submit with two fields $_POST['osta_sodur'] and $_POST['osta_sodur1'] now i need to control does these numbers contains '-' or not. If not, proceed to other function's. If it contains '-' cancel the operation and say error to user.
i thinked something like that:

Code: Select all

if(strstr($_POST['osta_sodur'], '-') or strstr($_POST['osta_sodur1'], '-') ) {} else {}
but i can't get that code to work. :oops:

Posted: Mon May 28, 2007 1:46 am
by feyd
What, pray tell, is "nrs"?

Posted: Mon May 28, 2007 3:43 am
by boss01
numbers sorry my english is very bad.

Posted: Mon May 28, 2007 4:00 am
by onion2k
strpos()

Posted: Mon May 28, 2007 4:05 am
by Kieran Huggins
strpos() will return '0' which evaluates as false if the '-' is the first character in the string, which is likely. If it's numbers you're testing, why not try:

Code: Select all

if($_POST['osta_sodur']>=0 && $_POST['osta_sodur1']>=0 ) {
  /* both positive */
} else {
  /* one or both are negative */
}

Posted: Mon May 28, 2007 6:29 am
by onion2k
Kieran Huggins wrote:strpos() will return '0' which evaluates as false if the '-' is the first character in the string, which is likely.
So what?

Code: Select all

if (strpos($string,"-")!==FALSE) {
  //There be hyphens!
}

Posted: Mon May 28, 2007 7:55 am
by superdezign
Kieran Huggins wrote:strpos() will return '0' which evaluates as false if the '-' is the first character in the string, which is likely. If it's numbers you're testing, why not try:

Code: Select all

if($_POST['osta_sodur']>=0 && $_POST['osta_sodur1']>=0 ) {
  /* both positive */
} else {
  /* one or both are negative */
}
Wouldn't you want (int)$_POST['osta_sodur'] or intval($_POST['osta_sodur']) ?

Posted: Mon May 28, 2007 11:37 am
by Kieran Huggins
lol - "YARRRGH, THAR BE HYPHENS HERE!"

onion2k is technically correct, which is the best kind of correct ;-)

typecasting or using intval() is also a safe choice.