พอดีต้องการ function ที่ตัด tag html ออก ครั้งจะเขียนเองก็กว่าจะเสียเวลา เลยหาดูใน internet เลยถึงบางอ้อว่า ใน php เองมี function นี้อยู่แล้ว ซึ่งสามารถกำหนด allow tag ได้อีกด้วย คือ function strip_tags
แต่ก็ยังมีคนเขียน function ที่ทำงานในลักษณะเดียวกันนี้อีก ที่ได้ผลลัพธ์ใกล้เคียงกัน คือตัวอย่างด้านล่าง
<?php
function html2txt($document){
$search = array('@<script[^>]*?>.*?</script>@si', // Strip out javascript
'@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
'@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
'@<![\s\S]*?--[ \t
]*>@' // Strip multi-line comments including CDATA
);
$text = preg_replace($search, '', $document);
return $text;
}
//example
$test = "<h1><b><font color=red>Hello all</font></b></h1>";
echo html2txt($test);
?>
ที่มา: http://au2.php.net/manual/en/function.strip-tags.php จาก comment ที่อยู่ใน PHP Document.
strip_tags()