Home > Backend Development > PHP Tutorial > PHP implements formatting of multi-line text into JS usable format_PHP tutorial

PHP implements formatting of multi-line text into JS usable format_PHP tutorial

WBOY
Release: 2016-07-13 09:57:10
Original
955 people have browsed it

php implements formatted multi-line text into a usable format for Js

There are many scenarios for storing and writing templates in js, such as: html code templates for pop-up boxes, etc., which are not supported by js The way to write long text with line breaks must be added up line by line, such as:

The code is as follows:


var content = '
row 1
'
'
row 2
';

but cannot be written as:

The code is as follows:


var content = '
row 1

row2
';

So I processed a small piece of php code to simplify the manual typing operation.

tojs.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

Str To Js String

输入格式化的文本:

$jsContent = '';

if(isset($_POST['content']) && $_POST['content']) {

$content = strtr(htmlspecialchars($_POST['content']), array("rn" => "n"));

$rows = explode("n", $content);

foreach($rows as $row) {

$jsContent .= ' '' . $row . "'
";

}

$jsContent{0} = ' ';

}

?>

格式化后的结果:

1 2 3

4

6 7 8 9 10
11 12
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
Str To Js String

Enter formatted text:

<🎜>$jsContent = '';<🎜> <🎜>if(isset($_POST['content']) && $_POST['content']) {<🎜> <🎜>$content = strtr(htmlspecialchars($_POST['content']), array("rn" => "n")); $rows = explode("n", $content); foreach($rows as $row) { $jsContent .= ' '' . $row . "'
"; } $jsContent{0} = ' '; } ?>

Formatted result:

http://www.bkjia.com/PHPjc/984498.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/984498.htmlTechArticlephp implements formatted multi-line text into Js usable formats. There are many scenarios for saving and writing templates in js, such as: pop-up html code templates for boxes, etc. js does not support long text writing with line breaks, and one line is required...
Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template