วันนี้เคนพัฒนาเว็บไซต์ด้วย Codeigniter Framework และต้องการเอา mpdf มาใช้เพื่อสร้าง pdf ครับ โดยมีคนพัฒนาไว้ให้สำหรับ Codeigniter เรียบร้อยแล้ว สามารถไปโหลดได้ที่ Github เลย

แต่ปัญหาที่เจอหลังจากเอามาใช้งานก็คือ มีการขึ้น Error แบบนี้ครับ

Message: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead

Filename: includes/functions.php

Line Number: 97

ก็คือบอกว่าฟังก์ชั่น  preg_replace() เนี่ยมันถูกเลิกใช้งานไปแล้วนะ ให้ใช้ฟังก์ชั่น preg_replace_callback แทน

แต่ว่าการใช้งาน ของทั้ง 2 ฟังก์ชั่นมันก็ดันต่างกันเนี่ยสิครับ จะมานั่งแก้ก็งง ๆ กันไประยะนึง แต่พอไปค้นหาใน Google ก็มีคนแก้ปัญหาไว้ให้เรียบร้อยแล้วครับ 🙂

สามารถไปดูต้นฉบับของคนตอบได้ที่ StackOverflow ครับ

วิธีแก้ปัญหา


ไปที่ไฟล์ functions.php ในโฟลเดอร์ mpdf ครับ แล้วมองหา function ชื่อ strcode2utf($str,$lo=true) ของเคนจะอยู่บรรทัดที่ 92 ครับ

ของเดิม

if(!function_exists('strcode2utf'))
{   
    function strcode2utf($str,$lo=true) 
    { 
        //converts all the &#nnn; and &#xhhh; in a string to Unicode 
        if ($lo) { $lo = 1; } else { $lo = 0; }
 
        $str = preg_replace('/\&\#([0-9]+)\;/me', "code2utf('\',{$lo})",$str); 

        $str = preg_replace('/\&\#x([0-9a-fA-F]+)\;/me', "codeHex2utf('\',{$lo})",$str); 

        return $str;  
    }
}

ตรงตัวแปร $str ทั้ง 2 จุด ก็เอาโค้ดชุดนี้ไปแทนที่ได้เลยครับ

$str = preg_replace_callback('/\&\#([0-9]+)\;/m', function($m) use ($lo){return code2utf($m[1],$lo); }, $str);

$str = preg_replace_callback('/\&\#x([0-9a-fA-F]+)\;/m', function($m) use ($lo){return codeHex2utf($m[1],$lo);}, $str);

ก็จะสามารถใช้งานได้ปกติเลย

0 0 votes
Article Rating
0
Would love your thoughts, please comment.x
()
x