single and double quote in values?

Discussion in 'PHP' started by mahmood, Jun 21, 2006.

  1. #1
    How can I put some values in input tag if the value contains single or double quotes?
    for example lets say I have this sentens: A du"mmy exam'ple

    If I put single quote around the value, "ple" will be disappeard, if double quote, "mmy exam'ple" will be disappeard.

    Is there a solution?
     
    mahmood, Jun 21, 2006 IP
  2. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #2
    One of these should do the job.
    
    $x = 'A du"mmy exam\'ple';
    $x = "A du\"mmy exam'ple";
    
    PHP:
     
    exam, Jun 21, 2006 IP
  3. ahkip

    ahkip Prominent Member

    Messages:
    9,205
    Likes Received:
    647
    Best Answers:
    0
    Trophy Points:
    310
    #3
    PUT \ before single quote
     
    ahkip, Jun 21, 2006 IP
  4. mahmood

    mahmood Guest

    Messages:
    1,228
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The problem is that I don't know what character may be in.

    I just used str_replace to replace dobule quote with " and it worked fine. Is there a function to do all the work without replacing problematic characters all together?
     
    mahmood, Jun 21, 2006 IP
  5. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #5
    PHP offers the following solution:

    
    <?php
    $str = "A 'quote' is <b>bold</b>";
    
    // Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;
    echo htmlentities($str);
    
    // Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;
    echo htmlentities($str, ENT_QUOTES);
    ?>
    
    Code (markup):
     
    clancey, Jun 21, 2006 IP
  6. vishwaa

    vishwaa Well-Known Member

    Messages:
    271
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    138
    #6
    htmlspecialchars(stripslashes($_POST['textfield']))
    PHP:
    should work as you expected.
     
    vishwaa, Jun 21, 2006 IP
  7. enampwd

    enampwd Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    We know double quote has some extended functionality over single quote. Like:
    $var = 'hello world!';
    echo "$var\n";

    This will print: hello world!. This variable replacement is not available with single quote.

    My question is which is faster single quote or double quote? Which one is preferable to use?
     
    enampwd, Jun 25, 2006 IP
  8. daworm

    daworm Peon

    Messages:
    121
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Single quotes are faster. Double quotes will parse $variable that are found inside it.

    And
    $str ="Hello my name is ".$name is faster than
    $str ="Hello my name is $name";

    though negligible in a sense but would matter when you have a very long string to parse
     
    daworm, Jun 25, 2006 IP