...

View Full Version : checking string for '



Hatch
06-21-2002, 06:35 PM
I'm using php.
I have a text box in a form and I want to check if they have typed in a ' charactor in that field. How do i do that in php.
Thanks

SYP}{ER
06-21-2002, 07:51 PM
if (strstr ($string_to_search, "'")!=false){
//Yup, they typed it.
}else{
//Nope, they didn't type it.
}

Note that if you're trying to comment them out, you can use
addslashes() or to remove the slashes, you can use
stripslashes() :)

Jeewhizz
06-22-2002, 02:24 PM
aaron was nearly right... tho one error.. use this code :)



if (strstr ($string_to_search, "'")!==false){
//Yup, they typed it.
}else{
//Nope, they didn't type it.
}


Jee

P.s. junior members eh ;) :p

SYP}{ER
06-26-2002, 12:03 AM
Hey, what was my mistake? the !== instead of !=?

Well check THIS out, mista:

http://www.aaron-wright.com/grr.php

:p

If I'm still wrong, please clarify. lol

ReadMe.txt
06-28-2002, 11:33 PM
i'd have to say that aarons right !== is not correct AFAIK, unless PHP has been changed

SYP}{ER
06-28-2002, 11:52 PM
Jee may be right actually, but only if you're using the latest version of PHP :) I heard there were some changes being made and that we'll all have to get used to them, but then again I could be terribly misinformed and maybe there isn't even a new version of PHP :eek:

I need to sit down.

Thejavaman1
06-29-2002, 12:14 AM
or just


if(strstr($string_to_search, "'")){

//Yup, they typed it.

}else{

//Nope, they didn't type it.

}


because not false is the same as true... ;)

mordred
06-30-2002, 04:48 PM
...but being exact, the return value of strstr() isn't a boolean true either - it's only casted to true in the if-statement.

Apparently Jeewhizz confused strstr() with strpos(), because with strpos() you have to be wary of a returned 0 which gets casted to false, that's why you should use !== instead of !=.

http://www.php.net/manual/en/function.strpos.php

Phip
06-30-2002, 11:30 PM
*reads post and laughs to self*



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum