rv20 Posted June 1, 2009 Share Posted June 1, 2009 How do i check a string to see if it contains single or double quotes. I think this fucntion checks if it is alpha numeric which i don't want i just want to check for ' and " jsut incase you answer with the followng function, if (preg_match('/[^a-z0-9]/i', $username)) { return false;//quitting a function die('invalid characters');//quit execution } Quote Link to comment https://forums.phpfreaks.com/topic/160481-check-for-single-or-double-quotes-in-a-string/ Share on other sites More sharing options...
grissom Posted June 1, 2009 Share Posted June 1, 2009 Hi rv20 REGEX isn't my strong point, but try this for stripping out single and double quotes from your string $stripped_string = preg_replace('/\'\"/', '', $original_string); (the middle parameter is two single quotes, not one double one) if ($stripped_string == $original_string) { // then there can't have been any quotes in the first place ... Maybe someone better than me at regular expressions can advise as to whether or not I got that first expression right. Quote Link to comment https://forums.phpfreaks.com/topic/160481-check-for-single-or-double-quotes-in-a-string/#findComment-846868 Share on other sites More sharing options...
aschk Posted June 1, 2009 Share Posted June 1, 2009 What about: <?php if(strpos("'", $str) > 0 OR strpos('"', $str) > 0){ // contains either " or ' } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160481-check-for-single-or-double-quotes-in-a-string/#findComment-846870 Share on other sites More sharing options...
rv20 Posted June 1, 2009 Author Share Posted June 1, 2009 What about: <?php if(strpos("'", $str) > 0 OR strpos('"', $str) > 0){ // contains either " or ' } ?> Doesn't work, doesn't detect anything, i don't want to strip out the ' or " i want to detect if the input contains them and i can the send a reply back saying invalid username etc.. Quote Link to comment https://forums.phpfreaks.com/topic/160481-check-for-single-or-double-quotes-in-a-string/#findComment-846957 Share on other sites More sharing options...
SreejuS Posted April 17, 2013 Share Posted April 17, 2013 (edited) Use <?php if(strpos($value, "'") > 0 || strpos($value, '"') > 0) { return false; // if found } ?> Edited April 17, 2013 by SreejuS Quote Link to comment https://forums.phpfreaks.com/topic/160481-check-for-single-or-double-quotes-in-a-string/#findComment-1425239 Share on other sites More sharing options...
DavidAM Posted April 17, 2013 Share Posted April 17, 2013 Use <?php if(strpos($value, "'") > 0 || strpos($value, '"') > 0) { return false; // if found } ?> It would have to be >= not >. The first character in a string is at position zero. Quote Link to comment https://forums.phpfreaks.com/topic/160481-check-for-single-or-double-quotes-in-a-string/#findComment-1425245 Share on other sites More sharing options...
Christian F. Posted April 17, 2013 Share Posted April 17, 2013 (edited) Due to the type autocasting rules, I'm afraid neither would work. php > var_dump (false >= 0); bool(true)The proper test would read like this. As you'll have to check for type as well, and not just equality: if (strpos ($value, '"') !== false || strpos ($value, "'") !== false) { return true; } Edited April 17, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/160481-check-for-single-or-double-quotes-in-a-string/#findComment-1425286 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.