htmlEncode:function (html){
//1.首先动态创建一个容器标签元素,如DIV
var temp = document.createElement ("div")
//2.然后将要转换的字符串设置为这个元素的innerText(ie支持)或者textContent(火狐,google支持)
(temp.textContent != undefined ) ? (temp.textContent = html) : (temp.innerText = html)
//3.最后返回这个元素的innerHTML,即得到经过HTML编码转换的字符串了
var output = temp.innerHTML
temp = null
return output
},
/*2.用浏览器内部转换器实现html解码*/
htmlDecode:function (text){
//1.首先动态创建一个容器标签元素,如DIV
var temp = document.createElement("div")
//2.然后将要转换的字符串设置为这个元素的innerHTML(ie,火狐,google都支持)
temp.innerHTML = text
//3.最后返回这个元素的innerText(ie支持)或者textContent(火狐,google支持),即得到经过HTML解码的字符串了。
var output = temp.innerText || temp.textContent
temp = null
return output
},
/*3.用正则表达式实现html转码*/
htmlEncodeByRegExp:function (str){
var s = ""
if(str.length == 0) return ""
s = str.replace(/&/g,"&")
s = s.replace(/
s = s.replace(/>/g,">")
s = s.replace(/ /g," ")
s = s.replace(/\'/g,"'")
s = s.replace(/\"/g,""")
return s
},
/*4.用正则表达式实现html解码*/
htmlDecodeByRegExp:function (str){
var s = ""
if(str.length == 0) return ""
s = str.replace(/&/g,"&")
s = s.replace(/</g,"<")
s = s.replace(/>/g,">")
s = s.replace(/ /g," ")
s = s.replace(/'/g,"\'")
s = s.replace(/"/g,"\"")
return s
}
}
function HTMLEncode(html) {
var temp = document.createElement("div")
(temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html)
var output = temp.innerHTML
temp = null
return output
}
var tagText = "<p><b>123&456</b></p>"
console.log(HTMLEncode(tagText))//<p><b>123&456</b></p>
function HTMLDecode(text) {
var temp = document.createElement("div")
temp.innerHTML = text
var output = temp.innerText || temp.textContent
temp = null
return output
}
var tagText = "<p><b>123&456</b></p>"
var encodeText=HTMLEncode(tagText)
console.log(encodeText) //<p><b>123&456</b></p>
console.log(HTMLDecode(encodeText)) //<p><b>123&456</b></p>
把html转成PDF:1.下载HTML2FPDF开源软件,将其放在你的网站根目录文件夹下,比如 /srv/httpd/htdocs/test/;
2.新建1.php文件,1.php中的代码如下:
<?php
require('html2fpdf.php')
$pdf=new HTML2FPDF()
$pdf->AddPage()
// yourfile.html 你要转化的HTML文件
$yourfile_html = "yourfile.html"
// yourfile.pdf 转化成功后的pdf文件名
$yourfile_pdf = "yourfile.pdf"
$fp = fopen($yourfile_html,"r")
$strContent = fread($fp, filesize($yourfile_html))
fclose($fp)
$pdf->WriteHTML($strContent)
$pdf->Output($yourfile_pdf )
echo "PDF 文件成功生成!"
3.将你要转化的yourfile.html文件也放在test目录下,随后在浏览器中访问1.php文件就可以把html文件转化成PDF文件了,
注意事项:1.php,yourfile.html和html2fpdf.php都要在同一目录下