简体   繁体   中英

html symbols are getting replaced by ascii codes in javascript

I am having a php string in the following format:

<pre>
$configOptions = "vCPUCores : '1 vCPU $3.00AUD',Ram : '0.5 GB $10.00AUD',PrimaryDrive : '50GB',DriveIOPs : '100'";
</pre>

I need to pass this string to analytics using javascript.

  <script>
           var configOptions = <?php echo $configOptions; ?>;
    </scrip>

However, when the data is passed to analytics, the single quotes are replaced by the corresponding ascii codes -&#039;

The output is obtained in the following format:

var configOptions = vCPUCores : '1 vCPU3.00AUD',Ram : '0.5 GB10.00AUD',PrimaryDrive : \'50GB\'

Here all the single quotes have got replaced ( when viewed through the browser source). This doesn't occur if I print the same in php. The issue occurs when the php data is passed to javascript.

I have tried several javascript and php encoding techniques regarding this, But none of them work.

Can anyone help me to fix this issue.

Try this:

<script>
       var configOptions = <?php echo urlencode($configOptions); ?>;
</scrip>

or

<script>
       var configOptions = <?php echo rawurlencode($configOptions); ?>;
</scrip>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM