Home > Backend Development > PHP Tutorial > 数据库的文本,如何能改为html代码

数据库的文本,如何能改为html代码

WBOY
Release: 2016-06-13 11:13:09
Original
1098 people have browsed it

数据库的文本,怎么能改为html代码?<br>这段代码,是可以把文本里的[img]<br>读成html。可以看到图,<br><br>但是,换行

<br>
Copy after login
Copy after login

却无法转换,直接就显示文本。
<br>
Copy after login
Copy after login

请问该怎么修改,才能让数据库里的换行展示换行,而不是文本。

$str=$content;

$count=preg_match_all('/\[img\](.*?)\[\/img\]/i',$str,$match, PREG_SET_ORDER);
if($count>0){
for($i=0;$i<$count;$i++){

// echo $match[$i][0].",".$match[$i][1]."<br>";
$str1="<br><br>";
//echo $str1.$match[$i][0];
$str = str_replace($match[$i][0], $str1, $str);

}
$bodytag=$str;
} else {
$bodytag=$str;
}

$count=preg_match_all('/\[url\ssrc=(((ht){1}tp://)[-a-zA-Z0-9@:%_/+.~#?&//=]+)\](.*?)\[\/url\]/i',$bodytag,$match, PREG_SET_ORDER);

if($count>0){
for($i=0;$i $str2="".$match[$i][4]."";
$bodytag = str_replace($match[$i][0], $str2, $bodytag);
}
$bodytag1 =$bodytag;
} else {
$bodytag1 =$bodytag;
}
echo $bodytag1;
//echo $content;
?>
Copy after login

<br>------解决方案--------------------<br>如果是纯文字,显示时用nl2br就行<br>图文混排或者带标签格式就要慎重,避免<br>插入到某个标签内部,可能需要一个相对精确的正则<br>------解决方案--------------------<br>echo nl2br($bodytag1); 
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