Skip to content Skip to sidebar Skip to footer

Php/html - Triple Nesting Quotes

I know that similar questions have been asked on Stack Overflow many times, but I am having problems with triple nested quotes in html/php. I have looked at numerous questions, but

Solution 1:

Why not use php heredoc and skip the hassle of escaping quotes? i.e.:

echo <<< EOF
<divid = 'feed-element'><buttonclass='username-button'type='button'>@{$currentUsername}</button><buttonclass='hashtag-one-button'type='button'>{$hashtag_one}</button><buttonclass='hashtag-two-button'type='button'>{$hashtag_two}</button><buttonclass='play-button'id='play-button{$i}'type='button'onclick='changeImage(this.id,{$track_url})'></button><buttonclass='email-button'type='button'>Contact: {$email}</button></div>
EOF;

Note: The curly braces are optional but may help code readability.

Solution 2:

For your error-causing code, you need to escape double quotes, not single:

<button class='play-button'id='play-button".$i."'type='button' onclick='changeImage(this.id,\"".$track_url."\")'></button>

Because you are using double quotes, you don't need to concatenate. Just insert the variable and away you go!

echo"<divid='feed-element'><buttonclass='username-button'type='button'>@$currentUsername</button><buttonclass='hashtag-one-button'type='button'>$hashtag_one</button><buttonclass='hashtag-two-button'type='button'>$hashtag_two</button><buttonclass='play-button'id='play-button$i'type='button'onclick='changeImage(this.id,\' $track_url\ ')'></button><buttonclass='email-button'type='button'>Contact: $email</button></div>";

Solution 3:

For using quotes to any level in PHP/HTML, use forst level as either single or double quote. After that you have two options. 1. Use double quotes 2. Use single quotes with backslash before the quote. For example, echo "This is 'In quotes'"; or echo "This is \"In quotes\"";

Solution 4:

In order to have multiple type of quotes on a line of code use . Example :

echo'It\'s me, hey';

Solution 5:

You'e all crazy. Just end the php block and write whatever then start it up again.


Example

I want to dynamically create 3 different div elements, each one with two parameters: $ID and $TEXT which represent the dom element ID and the innerHTML.

Now to make it truely complex, I want to dynamically insert these elements into a Javascript Function, so that they will load when I call the JS function.

Here's how to do that: You simply end the PHP tag and then enter your desired content as if the PHP tag never existed, and it will parse it as if it was specified within PHP without having to escape anything

<?php/* define regular function to generate dynamic element with PHP */functioncreate_my_div($ID, $TEXT) {

  /* end the PHP tag and start just regularly entering code 
 ?>

    <div id='<?=$ID;?>'>
      <?php print_r(htmlspecialchars($TEXT)); ?>
    </div>

   <?php
   /* we started up the PHP tag again, followed by a } to end the function
}

?>

Now anytime we call create_my_div("someID", "some text"); with PHP it will create our DIV element.

Lets say we wanted to populate a javascript function's DIV elements server-side and put them into the Javascript Function create_my_divs()

We first would need to have a way to ensure that our DIV elements are properly escaped as mentioned in the other answers, which can be done with this PHP code:

<?phpfunctionescapeJavaScriptText($string)
{
    return str_replace("\n", '\n', str_replace('"', '\"', addcslashes(str_replace("\r", '', (string)$string), "\0..\37'\\")));
}

?>

And then finally, all we have to do is this on our web page:

<scripttype="text/javascript">/* target element is where the DIVS will be created in */functioncreate_my_divs(target_element) {
    target_element.innerHTML += "<?=escapeJavascriptText(create_my_div("DIV1", "THIS IS DIV1"));?>";
    target_element.innerHTML += "<?=escapeJavascriptText(create_my_div("DIV2", "THIS IS DIV2"));?>";
    target_element.innerHTML += "<?=escapeJavascriptText(create_my_div("DIV3", "THIS IS DIV3"));?>";


}
</script>

This method will allow you to include javascript code or whatever without worrying about triple nesting


Here's another use case for this method:

Dynamically adding Javascript code:

<?phpfunctionloop_start($varName) {
   ?>for (var i=0; i<<?php print_r($varName);?>.length; i++) {

   <?php
}

?>

Now your Javascript code could look like this:

<script><?php 
     loop_start("myArray");
     ?>console.log(myArray[i]);
     }
</script>

Which would result in the following to be rendered:

<script>for (var i=0; i<myArray.length; i++) {
        console.log(myArray[i]);
     }
</script>

Conclusion

Stop worrying about trying to triple escape or double escape, or even escape at all.

With the tricks outlined in this answer, you can avoid escaping all together.

(Escape the confusion if you will)

Post a Comment for "Php/html - Triple Nesting Quotes"