Single quotes enclosed in double quotes not showing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JnrJnr
    New Member
    • Oct 2009
    • 88

    Single quotes enclosed in double quotes not showing

    Hi all. Im very new to PHP so heres my q
    I have an array that has multiple different sentences in various indexes. The sentences contain (')apostrophes and
    (")double quotes.
    I want my array at whatever index to echo the sentece-
    The boat's anchor is "heavy".
    If I do this-
    Code:
    myArr[0] = "The boat's anchor is \"heavy\"."; 
    echo myArr[0];
    ...it cuts the sentence off at the (')apostrophe. If I do this-
    Code:
    myArr[0] = 'The boat\'s anchor is "heavy".';
    echo myArr[0];
    ...it cuts the sentence off at the (')apostrophe. If I remove the (')apostrophe both senteces echo correctly with the double quotes visible.

    I thaught this was true that
    Using double quotes in a string enclosed with single quotes requires no escaping and vica versa.
    Using a double quotes in a string enclosed in double quotes does require escaping.

    I've tried using the str_replace function to replace the apostrophe with HTML character code '
    Code:
    $str = str_replace("'", "'", myArr[0]); 
    echo $str;
    ...and it still cuts off at (')apostrophes.
    The only option that works so far is using html character codes in the sentece itself-
    Code:
    myArr[0] = "The boat's anchor is \"heavy\"."; 
    echo myArr[0];
    ...But this makes readability more difficult.
    Is there any other ways you can help me with solving my problem with the single quotes / (')apostrophes not displaying when the sentence is echo'd?
    Last edited by Dormilich; Jul 20 '11, 08:39 PM.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    It works fine for me:
    Code:
    php > $myArr = array("The boat's anchor is \"heavy\".");
    php > echo $myArr[0];
    The boat's anchor is "heavy".
    php > $myArr = array('The boat\'s anchor is "heavy".');
    php > echo $myArr[0];
    The boat's anchor is "heavy".
    I cannot think what would be causing that behaviour.

    Comment

    • JnrJnr
      New Member
      • Oct 2009
      • 88

      #3
      Hi Markus,
      Sory I have to correct my question alot!
      Im trying to...
      Code:
      echo $_Post(['value']);
      ...the value of the array not just echo it. I want to $_Post() the value of an html <select> (dropdownlist) that has its values and results based on the array. Here is my exact code:
      Code:
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
      <html> 
      <head> 
      <title>How To Detect If User Javascript Is Enabled by Wallpaperama.com</title> 
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
      </head> 
      <?php 
      $arrValues['-'] = "";
      $arrValues['one'] = "1";
      $arrValues['two'] = "2's";
      $arrValues['three'] = "3";
      $arrValues['four'] = "4's";
      ?>
      <body> 
      <form onSubmit="return gos()" method="post" action="test.php">
      <select id="selectID" name="selectName" onchange="this.form.submit()" style="width:400px">
         <?php 
         foreach ($arrValues as $key => $value)
      {
      if($value == $_POST['selectName'])
      		{$mySelection = "selected";}
      		else
      		{$mySelection = "";}
      echo "<option value = '".$value."'".$mySelection.">".$key."</option>";
      }
      
      ?>
      </select>
      <?php 
      echo $_POST['selectName']. '<br> <hr>';  
      foreach ($arrValues as $key => $value)
      {
      	if($value == $_POST['selectName'])
      		{$mySelection = "selected";}
      		else
      		{$mySelection = "";}
      		$posst = $_POST['selectName'] ;
      	echo 'key = ' . $key . '<br>' . 'value = ' . $value .'<br>' . 'Post = ' . $posst .'<br>' . 'mySelection value = ' . $mySelection . '<br> <hr>';
      }
      ?>
      </form>
      </body> 
      </html>
      You can see that echo $_POST['selectName']; at line 30 only gives me the value without the (')quotation mark and also cuts everything off after the quotation mark.

      What is your magic_quotes_gp c, magic_quotes_ru ntime and magic_quotes_sy base in your php.ini file set to? Mine is all set to off. Also what version of php do you have installed? mine is Version5.3.6

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Ah!

        You see, you're using single quotes on line 24 for the option's value attribute. When the PHP is executed, the HTML given to the browser is as follows: <option value='The boat's anchor is heavy' selected />

        Do you see the issue?

        Comment

        • JnrJnr
          New Member
          • Oct 2009
          • 88

          #5
          ahh I see. It should be...
          Code:
          echo '<option value = "'.$value.'"'.$mySelection.">".$key."</option>";
          But what if I use (")double quotes in my sentence? ie
          The boat's anchor is "heavy".
          Then it cuts off at the double quotes like it did with the single quotes.

          Comment

          Working...