Multiple lines of string in PHP are passed to JavaScript method two

Source: Internet
Author: User

Both PHP and JavaScript are beginners. There is a recent need:

For example, there is a multi-line string for PHP:

$a = <<<EOFthy38csdnblogEOF;

After passing to JavaScript is equivalent to:

var c= ' Thy38\ncsdn\nblog ';

Because the understanding of these two languages is low to do not know how Google. Just be good to find out two ways to do it yourself:

1. Escape PHP first. and then cut. Then go to JSON, then JavaScript parse, and then the concatenation with \ n.

var b=json.parse (<?

php echo ' \ '. Json_encode (Explode ("\ r \ n", $a)). ' \‘‘; ?

>). Join (' \ n '); alert (b==c);


2. Arrange a hidden input on the page and give it to PHP first. Then JavaScript reads from it, and it implements the delivery of multiple rows of values.
<input type= "hidden" id= ' testphp ' value= ' <?php echo $a?

> "/>var a=document.getelementbyid (" testphp "). Value;var b=json.parse (<?php echo ' \ '. Json_encode (Explode (" \ r \ n ", $a)). ' ?>). Join (' \ n '); alert (a==b); alert (b==c);


PS: The above method is in the discussion with a PHP and JavaScript program ape classmate gradually draw.

After writing the article back to think about it, the fact that the two very much around the method is also due to PHP and JavaScript multi-line string understanding. It 's easy to understand. Far from being so complicated, direct string substitution can
var d=<?php echo ' \ '. Str_replace ("\ r \ n", "\\n\\\n", $a). ' \‘‘; ?

>;alert (D==C);


Willing to share with those who have just started learning.

Multiple lines of string in PHP are passed to JavaScript method two

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.