tırnak ve hattı ile bir PHP değişkeni kodlayan bir Javascript işlevi geçirilecek tatili (ve daha sonra kodlama tersine)

0 Cevap

Eğer aşağıdaki değere (yani tırnak ve satır sonları içerir) ile $ açıklama adında bir PHP değişkeni var diyelim:

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.

Sen tip bir metin GIRIS içine değerini yazar bir JavaScript işlevi içine bu değişkenin içeriğini geçmek istiyorum.

Bunu nasıl yapardın? Ben bu çalıştı:

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

Ancak, ben bir JS hatası alıyorum. Ben de bu çalıştı:

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

Bu çok gibi değerini kodlar:

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

Bu değer bir JS değişken olarak geçmiş olabilir, ama ben temiz bir PHP rawurlencode tersine çevirecek bir JS fonksiyonu bilmiyorum.

Ve sonra ben PHP değişkenin orijinal değerini almak böylece JS kodlama ters - Ben bir JS işleve izin vermek için PHP bir değişken kodlamak için kullanabilirsiniz fonksiyonları eşleşen çifti var mı?

EDIT: soru açıklığa kavuşturmak ve yorumlara cevap için, burada bazı test kodu:

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

0 Cevap