1. 程式人生 > 其它 >pbootcms小程式解析頁面出現空白或者解析頁面失敗的問題

pbootcms小程式解析頁面出現空白或者解析頁面失敗的問題

背景:在pbootcms與小程式互通的過程中,使用的是wxParse進行解析html頁面,因為小程式不支援複雜的樣式,所以有的時候小程式頁面會顯示空白等解析失敗問題,這裡就是為了解決這個問題

核心思路:

在請求api.php的過程中針對pb返回的頁面內容事先通過正則的方式進行去除一些不能解析的程式碼

處理步驟:

1)在\apps\api\controller\ContentController.php 中新增一行呼叫解析方法的程式碼

$data->content = KillStyle($data->content);
2)在\apps\common\function.php 中新增KillStyle方法

function KillStyle($content)
    {
        //echo"進入KillStyle====";
        //var_dump($content);
    $content = preg_replace("/STYLE=.+?['|\"]/i",'',$content);//去除樣式 
    $content = preg_replace("/style=.+?['|\"]/i",'',$content);//去除樣式
    //var_dump($content);
    $content = preg_replace("/class=.+?['|\"]/i",'',$content
);//去除樣式 $content = preg_replace("/id=.+?['|\"]/i",'',$content);//去除樣式 $content = preg_replace("/class=.+?['|\"]/i",'',$content);//去除樣式 $content = preg_replace("/id=.+?['|\"]/i",'',$content);//去除樣式 $content = preg_replace("/lang=.+?['|\"]/i",'',$content);//去除樣式 $content = preg_replace
("/width=.+?['|\"]/i",'',$content);//去除樣式 $content = preg_replace("/height=.+?['|\"]/i",'',$content);//去除樣式 $content = preg_replace("/border=.+?['|\"]/i",'',$content);//去除樣式 $content = preg_replace("/face=.+?['|\"]/i",'',$content);//去除樣式 $content = preg_replace("/face=.+?['|\"]/",'',$content);//去除樣式只允許小寫正則匹配沒有帶 i 引數 $content = preg_replace("/<a[^>]*>/i", "", $content); $content = preg_replace("/<\/a>/i", "", $content); $content = preg_replace("/<div[^>]*>/i", "", $content); $content = preg_replace("/<\/div>/i", "", $content); $content= preg_replace("/<!--[^>]*-->/i", "", $content);//註釋內容 $content= preg_replace("/|<\/p>/is","", $content); $content= preg_replace("/|<\/span>/is","", $content); $content = preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si","",$content); //過濾script標籤 return $content; }

栗子:

pc網站:

http://www.mybancai.cn/industry/107.html


上面紅圈部分,基本上是直接解析不了的

小程式頁面:

https://b5u7wv.smartapps.cn/pages/news-detail/news-detail?id=107