Need function like strstr

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
boss01
Forum Newbie
Posts: 19
Joined: Sun May 27, 2007 3:31 pm

Need function like strstr

Post 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:
Last edited by boss01 on Mon May 28, 2007 3:42 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What, pray tell, is "nrs"?
boss01
Forum Newbie
Posts: 19
Joined: Sun May 27, 2007 3:31 pm

Post by boss01 »

numbers sorry my english is very bad.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

strpos()
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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 */
}
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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!
}
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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']) ?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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.
Post Reply