Home >Backend Development >PHP Tutorial >Use php to escape output HTML to JavaScript_PHP tutorial

Use php to escape output HTML to JavaScript_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:59:49776browse

Use php to escape and output HTML to JavaScript

This article shares with you a small requirement in a personal project. I need to use php to escape and output HTML to JavaScript, so I wrote it. This function is recommended to everyone, I hope everyone likes it.

Recently, I am working on GIS integration for Sky Map. It is necessary to output HTML to JavaScript. It involves code escaping. It is quite troublesome. So I wrote a PHP function

Share it:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

function jsformat($str)

{

$str = trim($str);

$str = str_replace('\s\s', '\s', $str);

$str = str_replace(chr(10), '', $str);

$str = str_replace(chr(13), '', $str);

$str = str_replace(' ', '', $str);

$str = str_replace('\', '\', $str);

$str = str_replace('"', '\"', $str);

$str = str_replace('\'', '\'', $str);

$str = str_replace("'", "'", $str);

return $str;

}

1 2

3

4

5 6

78 9 10 11 12 13
function jsformat($str)
{ $str = trim($str); $str = str_replace('\s\s', '\s', $str); $str = str_replace(chr(10), '', $str); $str = str_replace(chr(13), '', $str); $str = str_replace(' ', '', $str); $str = str_replace('\', '\\', $str); $str = str_replace('"', '\"', $str); $str = str_replace('\'', '\\'', $str); $str = str_replace("'", "'", $str); return $str; }
Needless to say, just call jsformat($str) directly The above is the entire content of this article. I hope it will be helpful for everyone to understand php escaping to javascript http://www.bkjia.com/PHPjc/975127.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/975127.htmlTechArticleUse php to escape and output HTML to JavaScript. This article shares with you a small requirement in a personal project, which needs to be used PHP escapes and outputs HTML to JavaScript, so I wrote a function, recommended to everyone...
Statement:
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