From 55552fb296209ae65b3c56db062c28d93cc4ab64 Mon Sep 17 00:00:00 2001 From: Kengwang <1136772134@qq.com> Date: Wed, 1 Apr 2020 23:01:22 +0800 Subject: =?UTF-8?q?=F0=9F=94=A7=20Fixed:=20=E5=A4=9A=E4=B8=AAJSON=E5=8F=8B?= =?UTF-8?q?=E9=93=BE=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 这次为了效率我改成了str_replace (代码强迫症福音!) * 不小心格式化了下 (Phpstorm的格式化格式咋改啊QAQ) --- libs/Contents.php | 91 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 47 insertions(+), 44 deletions(-) (limited to 'libs/Contents.php') diff --git a/libs/Contents.php b/libs/Contents.php index b270059..06e7246 100644 --- a/libs/Contents.php +++ b/libs/Contents.php @@ -63,17 +63,18 @@ class Contents return $text; } - - /** - * 解析友链 - */ - static public function parseLink($text){ - //解析友链盒子 - $reg = '/\[links\](.*?)\[\/links\]/s'; + + /** + * 解析友链 + */ + static public function parseLink($text) + { + //解析友链盒子 + $reg = '/\[links\](.*?)\[\/links\]/s'; $rp = ''; - $text = preg_replace($reg,$rp,$text); + $text = preg_replace($reg, $rp, $text); //解析友链项目 - $reg = '/\[(.*?)\]\{(.*?)\}\((.*?)\)\+\((.*?)\)/s'; + $reg = '/\[(.*?)\]\{(.*?)\}\((.*?)\)\+\((.*?)\)/s'; $rp = ''; - $text = preg_replace($reg,$rp,$text); - //解析外部友链 - $reg = '/\[links data="(.*?)"\]/'; - $dataLink = preg_match($reg,$text,$matches); - if (!$dataLink) return $text; //普通文章别匹配! - $http=Typecho_Http_Client::get(); + $text = preg_replace($reg, $rp, $text); + //解析外部友链 + $reg = '/\[links data="(.*?)"\]/'; + $dataLink = preg_match_all($reg, $text, $matches); + if (!$dataLink) return $text; //普通文章别匹配! + $http = Typecho_Http_Client::get(); if (false == $http) { - $text=preg_replace($reg,'
对不起, 您的主机不支持 php-curl 扩展而且没有打开 allow_url_fopen 功能, 无法正常使json友链功能',$text); + $text = str_replace($matches[0][0], '
对不起, 您的主机不支持 php-curl 扩展而且没有打开 allow_url_fopen 功能, 无法正常使json友链功能', $text); return $text; } - try { - $result = $http->send($matches[1]); - }catch (Typecho_Http_Client_Exception $ex){ - $text=preg_replace($reg, '对不起,json外链请求失败! 错误信息:'.$ex->getMessage(),$text); - return $text; - } - $data = json_decode($result, true); - if ($data==false) {//没获取到数据就别走了!赶紧返回免得搞出事情 - $text=preg_replace($reg, '对不起,json外链解析失败!',$text); - return $text; - } - $linkItemNum = count($data); - $linksList='';//先Define一下,IDE在报错QAQ - for ($i = 0; $i < $linkItemNum; $i++) { - $name = $data[$i]['name']; - $link = $data[$i]['link']; - $avatar = $data[$i]['avatar']; - $des = $data[$i]['des']; - //嵌入列表 - $linksList .= '