Home > Backend Development > PHP Tutorial > 使用php转义输出HTML到JavaScript

使用php转义输出HTML到JavaScript

PHPz
Release: 2018-10-09 17:52:36
Original
897 people have browsed it

最近在做天地图是GIS集成··要输出HTML到JavaScript里面··涉及到代码转义什么的比较麻烦··所以写个PHP的function

分享一下:

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;
}
Copy after login

使用就不用说了··就是直接调用jsformat($str)

以上所述就是本文的全部内容了,希望能对大家理解php转义到javascript有所帮助

请您花一点时间将文章分享给您的朋友或者留下评论。我们将会由衷感谢您的支持!

更多相关教程请访问 php编程从入门到精通全套视频教程

Related labels:
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