資料庫的文本,如何能改為html代碼

來源:互聯網
上載者:User
資料庫的文本,怎麼能改為html代碼?
這段代碼,是可以把文本裡的[img]
讀成html。可以看到圖,

但是,換行


卻無法轉換,直接就顯示文本。


請問該怎麼修改,才能讓資料庫裡的換行展示換行,而不是文本。

$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]."
";
$str1="

";
//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;
?>


------解決方案--------------------
如果是純文字,顯示時用nl2br就行
圖文混排或者帶標籤格式就要謹慎,避免
插入到某個標籤內部,可能需要一個相對精確的正則
------解決方案--------------------
echo nl2br($bodytag1);
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.