最近在做天地图是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);
来源gaodai$ma#com搞$$代**码网$str = str_replace(‘\\\”, ‘\\\\\”, $str);
$str = str_replace(“‘”, “\'”, $str);
return $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);
来源gaodai$ma#com搞$$代**码网$str = str_replace(‘\\\”, ‘\\\\\”, $str);
$str = str_replace(“‘”, “\'”, $str);
return $str;
}
使用就不用说了,就是直接调用jsformat($str)
以上就是PHP中输出转义JavaScript代码的实现代码的详细内容,更多请关注gaodaima搞代码网其它相关文章!