溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php string如何轉(zhuǎn)為html

發(fā)布時(shí)間:2021-09-02 10:17:32 來源:億速云 閱讀:147 作者:chen 欄目:編程語言

本篇內(nèi)容主要講解“php string如何轉(zhuǎn)為html”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“php string如何轉(zhuǎn)為html”吧!

php string轉(zhuǎn)為html的方法:1、通過htmlentities()將字符串中特殊字符轉(zhuǎn)化為html實(shí)體;2、通過htmlspecialchars()函數(shù)將字符串中特殊字符轉(zhuǎn)化為html實(shí)體。

本文操作環(huán)境:Windows7系統(tǒng)、PHP7.1版本、Dell G3電腦

php string怎么轉(zhuǎn)為html?

PHP中字符串與html相互轉(zhuǎn)化函數(shù)

在一般通信過程中,可能會(huì)遇到文檔文本編碼中所不包括很多字符,或者無法在鍵盤上輸入的字符。例如,版權(quán)符號(hào)(?)、分幣符號(hào)(¢)和語法重音符號(hào)等就屬于這種字符。為了克服這些缺點(diǎn),專門設(shè)計(jì)了一組統(tǒng)一的按鍵編碼,稱為字符實(shí)體引用。

1,htmlentities(),將字符串中特殊字符轉(zhuǎn)化為html實(shí)體。

string htmlentities(string $str,int $quote_style,int $charset,boolean $double_encode),因?yàn)闃?biāo)記中引號(hào)有特殊意義,可以通過$quote_style參數(shù)來選擇如何處理引號(hào),其值:ENT_COMPAT,轉(zhuǎn)換雙引號(hào),忽略單引號(hào),這是默認(rèn)值;ENT_NOQUOTES,忽略雙引號(hào)和單引號(hào);ENT_QUOTES,轉(zhuǎn)換雙引號(hào)和單引號(hào)。$charset參數(shù)確定轉(zhuǎn)換所用字符集,如果忽略charset,默認(rèn)認(rèn)為ISO-8859-1。$double_encode會(huì)阻止htmlentities()對(duì)字符串中已有的HTML實(shí)體編碼。

如:

<?php
$str = "Bill & 'Steve'";
echo htmlentities($str, ENT_COMPAT); // 只轉(zhuǎn)換雙引號(hào)
echo "<br>";
echo htmlentities($str, ENT_QUOTES); // 轉(zhuǎn)換雙引號(hào)和單引號(hào)
echo "<br>";
echo htmlentities($str, ENT_NOQUOTES); // 不轉(zhuǎn)換任何引號(hào)

輸出:

Bill & 'Steve'
Bill & 'Steve'
Bill & 'Steve'

頁面源代碼是:

Bill &amp; 'Steve'<br>Bill &amp; &#039;Steve&#039;<br>Bill &amp; 'Steve'
<?php
$str = "Bill & 'Steve'";
echo html_entity_decode($str, ENT_COMPAT); // 只轉(zhuǎn)換雙引號(hào)
echo "<br>";
echo html_entity_decode($str, ENT_QUOTES); // 轉(zhuǎn)換雙引號(hào)和單引號(hào)
echo "<br>";
echo html_entity_decode($str, ENT_NOQUOTES); // 不轉(zhuǎn)換任何引號(hào)
輸出:Bill & 'Steve'Bill & 'Steve'Bill & 'Steve'頁面源代碼是:
Bill & &#039;Steve&#039;<br>Bill & 'Steve'<br>Bill & &#039;Steve&#039;

2,htmlspecialchars()函數(shù),將字符串中特殊字符轉(zhuǎn)化為html實(shí)體。

string htmlspecialchars(string $str,int $quote_style,int $charset,boolean $double_encode),參數(shù)與htmlentities()函數(shù)中參數(shù)類似。

如:

<?php
$str = "This is some <b>bold</b> text";
$str1 = htmlspecialchars($str);
echo $str."<br/>";
echo $str1;

輸出:

This is some bold text
This is some <b>bold</b> text

源代碼如下:

This is some <b>bold</b> text<br/>This is some &lt;b&gt;bold&lt;/b&gt; text
htmlspecialchars()函數(shù)可以轉(zhuǎn)換的字符列表以及最終轉(zhuǎn)換格式如下:&=>&amp;"(雙引號(hào))=>&quot;'(單引號(hào))=>&#039;<=>&lt;>=>&gt;注:要防止用戶向一個(gè)交互式web應(yīng)用程序中輸入HTML標(biāo)簽,這個(gè)函數(shù)尤為有用。以下例子使用htmlspecialchars()來轉(zhuǎn)換可能有害的字符:
<?php
$input = "I just can't get <<enough>> of PHP!";
echo htmlspecialchars($input);

其反函數(shù)是htmlspecialchars_decode(),htmlspecialchars_decode(string $str,int $flag),$flags規(guī)定翻譯表將包含哪種引號(hào)以及翻譯表用于哪種文檔類型,默認(rèn)是ENT_COMPAT | ENT_HTML401。

如:

<?php
$str = "This is some <b>bold</b> text.";
$str1 = htmlspecialchars_decode($str);
echo $str."<br/>",$str1;

輸出:

This is some <b>bold</b> text.
This is some bold text.

源代碼:

This is some &lt;b&gt;bold&lt;/b&gt; text.<br/>This is some <b>bold</b> text.

3,strip_tags()函數(shù),從字符串中移除空字符,HTML和PHP標(biāo)記,將html轉(zhuǎn)化為純文本。

string strip_tags(string $str,string $allowable_tags) 

$str,待處理字符串,$allowable_tags指定不被去除的字符列表。

如:

<?php
$text  =  '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>' ;
echo  strip_tags ( $text );
echo  "\n" ;

// 允許 <p> 和 <a>
echo  strip_tags ( $text ,  '<p><a>' );

輸出:

php string如何轉(zhuǎn)為html

4,get_html_translation_table()函數(shù),返回使用htmlspecialchars()和htmlentities()后的轉(zhuǎn)換表。

array get_html_translation_table(int $table,int $flags,string $encoding),$table表示想要的表,分別是HTML_ENTITIES,HTML_SPECIALCHARS。$flags規(guī)定翻譯表將包含哪種引號(hào)以及翻譯表用于哪種文檔類型,默認(rèn)是ENT_COMPAT | ENT_HTML401。$encoding一個(gè)規(guī)定了要使用的字符集的字符串,默認(rèn)是ISO-8859-1。

如:

<?php
var_dump ( get_html_translation_table ( HTML_ENTITIES ,  ENT_QUOTES  |  ENT_HTML5 ));

輸出:

php string如何轉(zhuǎn)為html

5,阿薩德


到此,相信大家對(duì)“php string如何轉(zhuǎn)為html”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI