Home > Backend Development > PHP Tutorial > Escape JavaScript code with PHP output_PHP Tutorial

Escape JavaScript code with PHP output_PHP Tutorial

WBOY
Release: 2016-07-21 14:53:33
Original
818 people have browsed it

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

to share:

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 how to use it...just call jsformat($str) directly

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364759.htmlTechArticleRecently, I am working on Tianmap. It is a GIS integration that requires outputting HTML to JavaScript, which involves code escaping, so it is quite troublesome. Write a PHP function and share it: function jsformat($str) { $str...
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