Home Backend Development PHP Tutorial Two ways to pass multi-line strings in PHP to JavaScript_PHP Tutorial

Two ways to pass multi-line strings in PHP to JavaScript_PHP Tutorial

Jul 13, 2016 am 10:24 AM
transfer multiline string

PHP和JavaScript都是初学。最近有这么个需求:

比方说有一个PHP的多行字符串:

$a = <<<EOF 
thy38 
csdn 
blog 
EOF;
Copy after login

传递给JavaScript后要等价于:

var c='thy38\n\ 
csdn\n\ 
blog';
Copy after login

因为对这两门语言的理解低到不知如何Google,只好自己摸索出方法两则:

1. 将PHP先转义,然后分割,然后转JSON,然后JavaScript parse,最后用\n拼接。

var b=JSON.parse(<&#63;php echo '\''.json_encode(explode("\r\n", $a)).'\''; &#63;>).join('\n'); 
alert(b==c); 
Copy after login

2. 在页面上安排一个隐藏的input,然后先由php把值给它,然后JavaScript从它里面读,就实现了多行值的传递

<input type="hidden" id='testphp' value="<&#63;php echo $a&#63;>" /> 
var a=document.getElementById("testphp").value; 
var b=JSON.parse(<&#63;php echo '\''.json_encode(explode("\r\n", $a)).'\''; &#63;>).join('\n'); 
alert(a==b); 
alert(b==c); 
Copy after login

PS: 以上方法是在跟一个PHP加JavaScript程序员同学的讨论中逐步得出的。
写完了文章回头再想想,其实这两个很绕的方法还是源于对PHP和JavaScript的多行字符串理解不透。
理解透了也就简单了,远不用这么复杂,直接字符串替换就行:

var d=<&#63;php echo '\''.str_replace("\r\n", "\\n\\\n", $a).'\''; &#63;>; 
alert(d==c);
Copy after login

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/825410.htmlTechArticlePHP和JavaScript都是初学。最近有这么个需求: 比方说有一个PHP的多行字符串: $a = EOF thy38 csdn blog EOF; 传递给JavaScript后要等价于: var c='thy...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are Delivery Optimization files in Windows 11 and can you delete them? What are Delivery Optimization files in Windows 11 and can you delete them? Sep 29, 2023 pm 04:09 PM

Delivery Optimization is a feature that helps Windows Update and Windows Store run and deliver updates faster. Cache files in Delivery Optimization are supposed to be deleted after a while, but for some of our readers they keep piling up and taking up unnecessary space. Is it safe to delete delivery optimization files? Yes, it is safe to delete delivery optimization files, and in this article, you will find out how easy it is to do so in Windows 11. Although it is not recommended to manually delete delivery optimization files, it is possible to do so automatically. How to delete delivery optimization files on Windows 11? Click the search bar, type Disk Cleanup, and open the tool from the results. If you have multiple drives, select the drive with your system (usually C:

Horizontal concatenation of multiline strings in Python Horizontal concatenation of multiline strings in Python Aug 27, 2023 pm 11:49 PM

In Python, string concatenation is a common operation that allows you to combine two or more strings into a single string. While concatenating strings vertically (i.e., one below the other) is simple, concatenating strings horizontally (i.e., side by side) requires some extra processing, especially when dealing with multiline strings. In this article, we will explore different ways to perform lateral concatenation of multi-line strings in Python. Method1:Usingthe+OperatorThe+operatorcanbeusedtocombinetwoormorestringsintoasinglestring.However,whendea

How to use context to pass request parameters in Go How to use context to pass request parameters in Go Jul 22, 2023 pm 04:43 PM

The context package in the Go language is used to pass request context information in the program. It can pass parameters, intercept requests and cancel operations between functions across multiple Goroutines. To use the context package in Go, we first need to import the "context" package. Below is an example that demonstrates how to use the context package to implement request parameter passing. packagemainimport("context&quot

How to solve Vue error: Unable to use props to pass data How to solve Vue error: Unable to use props to pass data Aug 17, 2023 am 10:06 AM

How to solve Vue error: Unable to use props to pass data Preface: During the development process of Vue, it is very common to use props to transfer data between parent and child components. However, sometimes we may encounter a problem, that is, when using props to pass data, an error will occur. This article will focus on how to solve the error that props cannot be used to pass data in Vue. Problem description: In Vue development, when we use props in the parent component to pass data to the child component, if

Messaging in Java Messaging in Java Aug 26, 2023 pm 10:13 PM

Introduction Message passing is a method of transmitting communication between items or threads and is a fundamental idea in distributed systems and parallel programming. Depending on the specific needs of the implementation, message transfer in Java can be accomplished through various methods and structures using the power source java.util.concurrent container, which provides a series of interfaces and class libraries for establishing and handling threads as active locks And the synchronization mechanism is a single method in Java that implements message delivery, such as instances. For example, the Executor interface can be used immediately to execute tasks, while BlockingQueue connections can be used to pass statements between concurrent processes. The above is a flow chart of the entire process of message passing in Java. Interface typeExecu

How to pass PHP variables by reference How to pass PHP variables by reference Aug 26, 2023 am 09:01 AM

In PHP, you can use the ampersand (&) symbol to pass variables by reference instead of by value. This allows the original variable to be modified within a function or method. There are mainly two ways to pass PHP variables by reference: Using ampersand symbol Using ampersand symbol in function/method declaration Using ampersand symbol in function/method declaration When passing variables to function/method In PHP, you can use function/ The ampersand symbol (&) in a method declaration passes variables by reference. Here is the updated explanation: To pass a reference variable by using the & symbol in a function/method declaration, you need to include the & symbol before the parameter name in the function/method definition. This indicates that parameters should be passed by reference, allowing

How to deal with distributed transactions and message passing issues and solutions in C# development How to deal with distributed transactions and message passing issues and solutions in C# development Oct 10, 2023 am 11:42 AM

How to deal with distributed transactions and message passing problems and solutions in C# development. In distributed systems, distributed transactions and message passing are common problems. Distributed transactions refer to transactions involving multiple databases or services, while messaging refers to asynchronous communication between different components in the system. This article will introduce how to deal with these issues in C# development and provide specific code examples. 1. Distributed transaction problems and solutions In traditional single-node transactions, the transaction processing logic is encapsulated in a database operation. However, in distributed

How to use function argument passing in Go? How to use function argument passing in Go? May 11, 2023 pm 05:00 PM

Go is a strongly typed programming language, and its function parameters are passed by value. This means that when you pass a parameter to a function, you are actually making a copy of the parameter's value and passing that value to the function for processing. Therefore, when using function parameter passing in Go, you need to pay attention to the following points: The difference between value types and reference types In Go, except for the basic data type which is a value type, all data types are reference types. When value type data is passed as a function parameter, a copy of the value will be copied and passed to the function; while reference type data

See all articles