Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

encoding a PHP variable with quotes and line breaks to be passed to a Javascript function (and then reverse the encoding)

Tags:

php

Say you have a PHP variable called $description with the following value (that contains quotes and line breaks):

Tromp L'oeil Sheath Dress

You will certainly "trick the eye" of many in this gorgeous illusion. Add it to your fall wardrobe before it disappears.

You want to pass the contents of this variable into a Javascript function that writes that value into an INPUT of type text.

How would you do this? I tried this:

$description = htmlspecialchars ( $product->description, ENT_QUOTES );

However, I get a JS error. I also tried this:

$description = rawurlencode ( $product->description );

This encodes the value like so:

Michael%20Kors%0A%0ATromp%20L%27oeil%20Sheath%20Dress%0A%0AYou%20will%20certainly%20%22trick%20the%20ey%22%20of%20many%20in%20this%20gorgeous%20illusion.%20Add%20it%20to%20your%20fall%20wardrobe%20before%20it%20disappears.%0A%0AAvailable%20in%20Black%2FNude

This value can be passed as a JS variable, but I don't know of a JS function that will cleanly reverse a PHP rawurlencode.

Is there a matching pair of functions that I could use to encode a variable in PHP to allow it to be passed into a JS function -- and then reverse the encoding in JS so that I get the original value of the PHP variable?

EDIT: To clarify the question and reply to comments, here is some test code:

<?php
$str =<<<EOT
Tromp L'oeil Sheath Dress

You will certainly "trick the eye" of many in this gorgeous illusion. Add it to your fall wardrobe before it disappears.
EOT;
echo 'here is the string: <pre>' . $str . '</pre>';
?>

<script type="text/javascript">
<?php
// this does not work with JS as i get an unterminated string literal if i just use addslashes in the following commented-out line
// echo 'alert(\'' . addslashes($str) . '\');';

// this works with JS (the alert activates) but how do i un-rawurlencode in JS?
// echo 'alert(\'' . rawurlencode($str) . '\');';

// this does not work with JS, because of the line breaks
echo 'alert(\'' . htmlspecialchars ($str, ENT_QUOTES) . '\');';
?>
</script>
like image 944
bobbyh Avatar asked Jul 13 '10 18:07

bobbyh


Video Answer


2 Answers

simplest would be to use json_encode()

like image 78
Scott Evernden Avatar answered Nov 09 '22 22:11

Scott Evernden


I ran into problems using some of the answers proposed here, including issues with line breaks and decoding certain html entitites like /. I ended up using rawurlencode (in PHP) and decodeURIComponent (in Javascript) as matching functions to encode/decode the string so it could be passed as a JS variable. Here is working code for anybody else running into this problem.

<?php
$str =<<<EOT
Tromp L'oeil Sheath Dress

You will certainly "trick the eye" of many in this gorgeous illusion. Add it to your fall wardrobe before it disappears.
Available in Black/Nude
EOT;
echo 'here is the string: <pre>' . $str . '</pre>';
?>
<p>below is the variable doc.write'd after being rawurlencod'ed in PHP then decodeURIComponent'ed in JS:</p>
<script type="text/javascript">
<?php
echo 'document.write(decodeURIComponent("'. rawurlencode($str).'"));';
?>
like image 20
bobbyh Avatar answered Nov 09 '22 22:11

bobbyh