Jump to content

Check for single or double quotes in a string


rv20

Recommended Posts

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
}  

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 3 years later...

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 by Christian F.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.