Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping apostrophe in JavaScript or PHP [duplicate]

I have a foreach loop in PHP:

var circle_<?php echo $x; ?> = '<?php echo $c; ?>';

It returns this in JavaScript:

var circle_0 = '<p class="medium">With us, you can customize more than just a closet.</p>';

var circle_1 = '<p class="medium">We are boutique condo builders who love what we do.</p>';

var circle_2 = '<p class="medium">who says condos can't be spacious?<br/></p>';

As you can see, circle_2 has an apostrophe in the string, and as a result it breaks the script:

Uncaught SyntaxError: Unexpected token ILLEGAL

What filter would I use to fix this (PHP or JavaScript)?

like image 465
user990717 Avatar asked Dec 07 '22 08:12

user990717


2 Answers

Using PHP, you could try:

  <?php echo str_replace("'", "\'",$c);
like image 198
TommyBs Avatar answered Dec 24 '22 11:12

TommyBs


PHP addslashes() function will help you: http://php.net/manual/en/function.addslashes.php

like image 39
barbashov Avatar answered Dec 24 '22 13:12

barbashov