How to convert text box content to HTML format in PHP

墨辰丷
Release: 2023-03-29 09:54:01
Original
2957 people have browsed it

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;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!