php和html標(biāo)簽轉(zhuǎn)換的方法-創(chuàng)新互聯(lián)

php和html標(biāo)簽轉(zhuǎn)換的方法?這個(gè)問題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見到的。希望通過(guò)這個(gè)問題能讓你收獲頗深。下面是小編給大家?guī)?lái)的參考內(nèi)容,讓我們一起來(lái)看看吧!

創(chuàng)新互聯(lián)專注于鄂托克前網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供鄂托克前營(yíng)銷型網(wǎng)站建設(shè),鄂托克前網(wǎng)站制作、鄂托克前網(wǎng)頁(yè)設(shè)計(jì)、鄂托克前網(wǎng)站官網(wǎng)定制、微信小程序服務(wù),打造鄂托克前網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供鄂托克前網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。

php html標(biāo)簽轉(zhuǎn)換問題的解決辦法:1、使用“htmlentities()”函數(shù)將html標(biāo)簽轉(zhuǎn)換成特殊字符;2、使用“html_entity_decode()”函數(shù)將htmlentities函數(shù)轉(zhuǎn)義過(guò)的字符串轉(zhuǎn)成html標(biāo)簽。

php和html標(biāo)簽轉(zhuǎn)換的方法

很多朋友在寫php的時(shí)候,難免會(huì)遇到需要將html標(biāo)簽進(jìn)行轉(zhuǎn)義存儲(chǔ)。比如存入數(shù)據(jù)庫(kù)、xml文件等。而存儲(chǔ)進(jìn)去后,讀取出來(lái)則需要轉(zhuǎn)換成html輸出。網(wǎng)上有許多人編寫的轉(zhuǎn)換函數(shù),很長(zhǎng)很難懂。其實(shí)php早就自帶有這樣的函數(shù)。大可不必自己編寫。

下面分別介紹這兩個(gè)函數(shù)。

1.htmlentities()函數(shù):

說(shuō)明:將html標(biāo)簽轉(zhuǎn)換成特殊字符。例如將<script>轉(zhuǎn)換成"&lt;script&gt;"

例子:

[PHP] view plaincopy

  1. // An imaginary article submission from a bad user  
  2. //  it will redirect anyone to example.com if the code is run in a browser  
  3. $userInput = "I am going to hax0r your site, hahaha!  
  4.    <script type=&apos;text/javascript&apos;>  
  5.    window.location = &apos;http://www.example.com/&apos;  
  6.    </script>&apos;";  
  7.      
  8. //Lets make it safer before we use it  
  9. $userInputEntities = htmlentities($userInput);  
  10.  
  11. //Now we can display it  
  12. echo $userInputEntities;  

由于最近c(diǎn)sdn的控件比較垃圾,請(qǐng)將上面的$apos改成單引號(hào)。---呼!

上面的語(yǔ)句執(zhí)行后,將生成下面的結(jié)果

[HTML] view plaincopy

  1. I am going to hax0r your site, hahaha!  
  2.    <script type=&apos;text/javascript&apos;>  
  3.    window.location = &apos;http://www.88web.org/&apos;  
  4.    </script>&apos;  

2.html_entity_decode()函數(shù)

說(shuō)明:將htmlentities()函數(shù)轉(zhuǎn)義過(guò)的字符串轉(zhuǎn)成html標(biāo)簽。

例子:

[PHP] view plaincopy

  1. $orig = "I&apos;ll /"walk/" the <b>dog</b> now";  
  2.  
  3. $a = htmlentities($orig);  
  4.  
  5. $b = html_entity_decode($a);  
  6.  
  7. echo $a; // I will "walk" the <b>dog</b> now  
  8.  
  9. echo $b; // I will "walk" the <b>dog</b> now  

轉(zhuǎn)載自頁(yè)面     http://www.cankaojishu.com/bcyy/82144.html

感謝各位的閱讀!看完上述內(nèi)容,你們對(duì)php和html標(biāo)簽轉(zhuǎn)換的方法大概了解了嗎?希望文章內(nèi)容對(duì)大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道。

標(biāo)題名稱:php和html標(biāo)簽轉(zhuǎn)換的方法-創(chuàng)新互聯(lián)
鏈接分享:http://bm7419.com/article48/dsdgep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、做網(wǎng)站、網(wǎng)站制作、外貿(mào)建站軟件開發(fā)、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

小程序開發(fā)