一 代码
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>字符串与HTML相互转换</title> <style type="text/css"> <!-- .STYLE3 { font-size: 13px; color: #993300; } .STYLE4 { font-size: 20px; color: #993300; font-family: "新宋体"; font-weight: bold; } .STYLE5 {font-size: 13px; color: #993300; font-weight: bold; } --> </style> </head> <body> <table width="550" height="218" border="1" cellpadding="2" cellspacing="1" bordercolor="#33CC33" bgcolor="#CCFF33"> <tr> <td height="38" colspan="2" align="center"><span class="STYLE4">字符串转换成HTML</span></td> </tr> <tr> <td width="120" align="right" class="STYLE5">转换的结果为:</td> <td width="413" height="100" align="left"><span class="STYLE3"> <?php $str='<table width="300" border="1" cellpadding="1" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#993300"> <tr> <td align="center" bgcolor="#FFFFFF" class="STYLE3">做人做事</td> </tr> <tr> <td align="center" bgcolor="#FFFFFF" class="STYLE3">认认真真做事,坦坦荡荡做人!</td> </tr> </table>'; echo htmlentities($str,ENT_QUOTES,"GB2312")."<br>"; ?> </span> </td> </tr> <tr> <td align="right" class="STYLE5">不转换输出结果:</td> <td height="80" align="center"><span class="STYLE3"><?php echo $str; ?></span></td> </tr> </table> </body> </html>
二 运行结果
![](http://dl2.iteye.com/upload/attachment/0122/4530/b3dd6032-8dbd-3c91-8cd8-68780b7ebf69.png)
三 说明
htmlentities()函数将所有的字符串都转换为HTML字符。