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

Escape JavaScript code with PHP output_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 14:53:33763browse

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...
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