สอบถามเรื่องการตัด tags ของ BBCode

สำหรับผู้ที่ เริ่มต้น Programming - PHP มีอะไร แนะนำ หรือข้อสงสัยต้องบอร์ด นี้ คนที่มีความรู้ แบ่งปันคนอื่นบ้างนะ ปัญหาการเขียนโปรแกรม แบบ OOP Session Cookies php network

Moderator: mindphp, ผู้ดูแลกระดาน

flook
PHP VIP Members
PHP VIP Members
โพสต์: 3751
ลงทะเบียนเมื่อ: 06/06/2022 9:43 am

Re: สอบถามเรื่องการตัด tags ของ BBCode

โพสต์ที่ยังไม่ได้อ่าน โดย flook »

eange08 เขียน: 30/09/2022 2:21 pm ไปเจออีก function หนึ่งของ phpBB มาค่ะ ตัด bbcode ออกได้ เป็น

โค้ด: เลือกทั้งหมด

strip_bbcode(&$text, $uid = '')
//$text = ข้อความที่เป็น bbcode
//$uid = ส่ง bbcode_uid ของ post นั้นจะมีใน table phpbb_posts
ใน phpBB3.3 จะไม่ return ค่าออกมา เอาในฟังกชั่นมาใช้เองได้ค่ะ

โค้ด: เลือกทั้งหมด

public function hiddenBBCode($text, $uid) {
        global  $phpbb_root_path,$phpbb_container;


	if (preg_match('#^<[rt][ >]#', $text))
	{
		$text = utf8_htmlspecialchars($phpbb_container->get('text_formatter.utils')->clean_formatting($text));
	}
	else
	{
		if (!$uid)
		{
			$uid = '[0-9a-z]{5,}';
		}

		$text = preg_replace("#\[\/?[a-z0-9\*\+\-]+(?:=(?:&quot;.*&quot;|[^\]]*))?(?::[a-z])?(\:$uid)\]#", ' ', $text);

		$match = get_preg_expression('bbcode_htm');
		$replace = array('\1', '\1', '\2', '\1', '', '');

		$text = preg_replace($match, $replace, $text);
	}

		
	
        return $text;
    }
ก่อนใช้
Selection_999(152).png

หลังใช้
Selection_999(153).png
จากที่ลองวิธีนี้

ตัวอย่าง code function

โค้ด: เลือกทั้งหมด

 public function hiddenBBCode($text, $uid)
    {
        global $phpbb_container;


        if (preg_match('#^<[rt][ >]#', $text)) {
            $text = utf8_htmlspecialchars($phpbb_container->get('text_formatter.utils')->clean_formatting($text));
        } else {
            if (!$uid) {
                $uid = '[0-9a-z]{5,}';
            }

            $text = preg_replace("#\[\/?[a-z0-9\*\+\-]+(?:=(?:&quot;.*&quot;|[^\]]*))?(?::[a-z])?(\:$uid)\]#", ' ', $text);

            $match = get_preg_expression('bbcode_htm');
            $replace = '';

            $text = preg_replace($match, $replace, $text);
        }

        return $text;
    }
ตอนเรียกใช้

โค้ด: เลือกทั้งหมด

$this->hiddenBBCode($row['post_text'], $row['bbcode_uid'])
ผลลัพธ์

โค้ด: เลือกทั้งหมด

Solved asd [2022-09][00172]Solved asd [2022-09][00172] img_avatar2.png [attachment=0]img_avatar3.png[/attachment] img_avatar3.png 
ข้อมูลที่ใช้ทดสอบ

โค้ด: เลือกทั้งหมด

[b]Solved asd [2022-09][00172]Solved asd [2022-09][00172][/b]

[attachment=0]img_avatar2.png[/attachment]

[code]
[attachment=0]img_avatar3.png[/attachment]
[code]// ถ้าไปเป็น test จริงต้องใส่ / ด้วย

[attachment=0]img_avatar3.png[/attachment]


ได้ข้อมูลตามความต้องการครับ
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: Bing [Bot] และบุคลทั่วไป 116