This article mainly introduces the method of converting text box content into HTML format in PHP. It uses custom functions to convert strings into HTML format. It involves PHP's replacement skills for HTML tags. Friends in need can refer to the following
Sometimes we will use the content entered in the multi-line text box to be displayed in html format, so that the original text format, such as line feeds, carriage returns, etc., can be maintained. It can be achieved through the following function:
function shtm($design_str) { $str=trim($design_str); // 取得字串同时去掉头尾空格和空回车 //$str=str_replace("<br>","",$str); // 去掉<br>标签 //$str="<p>".trim($str); // 在文本头加入<p> $str=str_replace("\r\n","<br>",$str); // 用p标签取代换行符 //$str.="</p>\n"; // 文本尾加入</p> $str=str_replace("<p></p>","",$str); // 去除空段落 $str=str_replace("\n","",$str); // 去掉空行并连成一行 $str=str_replace("</p>","</p>\n",$str); //整理html代码 return $str; }
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.
Related recommendations:
File operation classes and file download functions implemented by PHP
Detailed graphic and text explanations of common PHP functions
php mysql operation mysql_connect connection database
##
The above is the detailed content of How to convert text box content to HTML format in PHP. For more information, please follow other related articles on the PHP Chinese website!