ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
~~~ <?php namespace App\Services; use Illuminate\Support\Str; /** * Created by nur.cn * info: 管理员活动记录 * Date: 2019/06/17 * Time: 16:13 */ class WxfilterConvertService { /** * 查询字符是否存在于某字符串 * @param string $haystack 字符串 * @param string $needle 要查找的字符 * @return bool */ private static function str_exists($haystack, $needle) { return !(stripos($haystack, $needle) === false); } /** * 删除由 addslashes() 函数添加的反斜杠 * @param array|string $string * @return array|string */ private static function new_stripslashes($string) { if (!is_array($string)) { return stripslashes($string); } foreach ($string as $key => $val) { $string[$key] = self::new_stripslashes($val); } return $string; } private static function filter_br($string){ return str_replace(array("\r\n", "\n", "\r"), ' ', $string); } /** * 清理多余标签 * @param string $data * @return mixed|string|string[]|null */ protected static function format_filter_data($data) { $data = self::filter_br(self::new_stripslashes($data)); $data = strip_tags($data, '<img>,<p>,<a>,<br>,<video>,<audio>,<iframe>'); $data = preg_replace('/(dir)=(\'|")(.*?)(\'|")/is', '', $data); $data = preg_replace('/(alt)=(\'|")(.*?)(\'|")/is', '', $data); $data = preg_replace('/(style)=(\'|")(.*?)(\'|")/is', '', $data); $data = preg_replace('/(title)=(\'|")(.*?)(\'|")/is', '', $data); $data = preg_replace('/(target)=(\'|")(.*?)(\'|")/is', '', $data); $data = preg_replace('/<p([^>]*>)/isU', '', $data); $data = preg_replace('/<br([^>]*>)/isU', "</p>", $data); $data = preg_replace('/<\/p([^>]*>)/isU', '</p>', $data); $data = str_replace('&nbsp;', ' ', $data); $data = preg_replace('/<\/video([^>]*>)/isU', '', $data); $data = preg_replace('/<\/audio([^>]*>)/isU', '', $data); $data = preg_replace('/<\/iframe([^>]*>)/isU', '', $data); return $data; } /** * 清理多余标签 * @param string $data * @return mixed|string|string[]|null */ protected static function format_filter_html($data) { $data = self::filter_br(self::new_stripslashes($data)); $data = strip_tags($data, '<img>,<p>,<a>,<br>,<video>,<audio>,<iframe>'); $data = preg_replace('/<br([^>]*>)/isU', "</p>", $data); $data = preg_replace('/<\/p([^>]*>)/isU', '</p>', $data); $data = str_replace('&nbsp;', ' ', $data); return $data; } /** * @param string $data * @param integer $nispet * @param string $http_img_url * @return mixed|string|string[]|null */ public static function content_convert3_array($data, $nispet, $http_img_url = '') { if (empty($nispet)) { $nispet = 30; } /* empty */ $data = self::format_filter_data($data); $data = preg_replace_callback( '|(<)(.*?)(>)|', function ($matches) { return str_replace(" ", "_nbsp_", $matches[0]) . " "; }, $data ); $data = explode(" ", filter_spaces($data)); $data = implode(" ", array_slice($data, 0, ($nispet / 100) * count($data))); $data = str_replace("_nbsp_", " ", $data); return self::content_elment_convert_array($data, $http_img_url); } /** * @param string $data * @param string $http_img_url * @return array */ public static function content_convert2_array($data, $http_img_url = '') { $data = self::format_filter_data($data); return self::content_elment_convert_array($data, $http_img_url); } /** * @param array $data_list * @return array */ public static function content_music_convert_array($data_list) { foreach ($data_list as $index => $item) { if ($item['type'] == 'audio') { preg_match('#src="(.*?)"#i', $item['value'], $matches); if (isset($matches['1'])) { $data_list[$index]['value'] = $matches['1']; $data_list[$index]['info'] = 'بۇ يەرنى بېسىپ ئاڭلاڭ'; } /* isset */ } /* end */ }/* foreach */ return $data_list; } /** * @param string $data * @return string|string[]|null */ protected static function filter_preg_replace($data) { $data = preg_replace('/<\s*a\s+[^>]*?href\s*=\s*(\'|\")(.*?)(\'|\")[^>]*?\/?\s*>/i', "<a href=\"$2\" >", $data); $data = preg_replace('/<\s*a\s+[^>]*?href\s*=\s*(\'|\")(.*?)(\'|\")[^>]*?\/?\s*><\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)(\'|\")[^>]*?\/?\s*><\/a>/i', "|safe_item_safe|[img_img]|$5|$2|image|safe_nbsp_safe", $data); $data = preg_replace('/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)(\'|\")[^>]*?\/?\s*>/i', "|safe_item_safe|[img_img]|$2|0|image|safe_nbsp_safe", $data); $data = preg_replace('/<\s*video\s+[^>]*?src\s*=\s*(\'|\")(.*?)(\'|\")\s+[^>]*?poster\s*=\s*(\'|\")(.*?)(\'|\")[^>]*?\/?\s*>/i', "|safe_item_safe|[mp4_mp4]|$2|$5|video|safe_nbsp_safe", $data); $data = preg_replace('/<\s*video\s+[^>]*?src\s*=\s*(\'|\")(.*?)(\'|\")[^>]*?\/?\s*>/i', "|safe_item_safe|[mp4_mp4]|$2|0|video|safe_nbsp_safe", $data); $data = preg_replace('/<\s*audio\s+[^>]*?src\s*=\s*(\'|\")(.*?)(\'|\")[^>]*?\/?\s*>/i', "|safe_item_safe|[audio_audio]|$2|0|audio|safe_nbsp_safe", $data); $data = preg_replace('/<\s*iframe\s+[^>]*?src\s*=\s*(\'|\")(.*?)(\'|\")\s+[^>]*?height\s*=\s*(\'|\")(.*?)(\'|\")[^>]*?\/?\s*>/i', "|safe_item_safe|[iframe_iframe]|$2|100%|$5|iframe|safe_nbsp_safe", $data); $data = preg_replace('/<\s*iframe\s+[^>]*?height\s*=\s*(\'|\")(.*?)(\'|\")\s+[^>]*?src\s*=\s*(\'|\")(.*?)(\'|\")[^>]*?\/?\s*>/i', "|safe_item_safe|[iframe_iframe]|$5|100%|$2|iframe|safe_nbsp_safe", $data); $data = preg_replace('/<\s*iframe\s+[^>]*?src\s*=\s*(\'|\")(.*?)(\'|\")[^>]*?\/?\s*>/i', "|safe_item_safe|[iframe_iframe]|$2|100%|400|iframe|safe_nbsp_safe", $data); return $data; } /** * @param string $content * @param string $http_img_url * @return array */ protected static function content_elment_convert_array($content, $http_img_url = '') { $list = array(); $content = explode('</p>', $content); foreach ($content as $index => $value) { if (trim($value) == '') { continue; } /* empty */ $value = self::filter_preg_replace($value); /* 格式化 */ $value = explode("safe_nbsp_safe", $value); foreach ($value as $key => $v) { if (!trim($v)) { continue; } /* empty */ if (self::str_exists($v, '|video|')) { $v = explode("|video|", $v); foreach ($v as $k => $m) { if (!trim($m)) { continue; } /* empty */ $m = explode("|safe_item_safe|", $m); foreach ($m as $i => $n) { if (!trim($n)) { continue; } /* empty */ if (self::str_exists($n, '[mp4_mp4]|')) { list($url, $poster) = explode("|", str_replace('[mp4_mp4]|', '', $n)); if (self::str_exists($url, '.nur.cn') || self::str_exists($url, '.ulinix.cn')) { $list[] = self::content_elment_format2('player', $http_img_url .$url, '', $poster); } else { //$list[] = self::content_elment_format('video', '<iframe src="' . $url . '" frameborder="0"></iframe>'); $list[] = self::content_elment_format2('player', $http_img_url .$url, '', $poster); } /* str_exists */ /*$list[] = self::content_elment_format('iframe', $url);*/ } else { $list[] = self::content_elment_format('text', self::convert_amp_gt_format($n)); } /* str_exists */ } /* foreach */ }/* foreach */ } elseif (self::str_exists($v, '|audio|')) { $v = explode("|audio|", $v); foreach ($v as $k => $m) { if (!trim($m)) { continue; } /* empty */ $m = explode("|safe_item_safe|", $m); foreach ($m as $i => $n) { if (!trim($n)) { continue; } /* empty */ if (self::str_exists($n, '[audio_audio]|')) { list($url, $time) = explode("|", str_replace('[audio_audio]|', '', $n)); $list[] = self::content_elment_format('audio', '<iframe src="' . $url . '" frameborder="0"></iframe>'); } else { $list[] = self::content_elment_format('text', self::convert_amp_gt_format($n)); } /* str_exists */ } /* foreach */ }/* foreach */ } elseif (self::str_exists($v, '|iframe|')) { $v = explode("|iframe|", $v); foreach ($v as $k => $m) { if (!trim($m)) { continue; } /* empty */ $m = explode("|safe_item_safe|", $m); foreach ($m as $i => $n) { if (!trim($n)) { continue; } /* empty */ if (self::str_exists($n, '[iframe_iframe]|')) { list($url, $w, $h) = explode("|", str_replace('[iframe_iframe]|', '', $n)); if (self::str_exists($url, 'https://haokan.baidu.com/') && self::str_exists($url, 'vid=')) { $list[] = self::content_elment_format2('player', _core_get_api_vmp2_url_convert($url, 'haokan_baidu')); } else { $list[] = self::content_elment_format('iframe', '<iframe src="' . $url . '" frameborder="0"></iframe>'); } /* str_exists */ } else { $list[] = self::content_elment_format('text', self::convert_amp_gt_format($n)); } /* str_exists */ } /* foreach */ }/* foreach */ } elseif (self::str_exists($v, '|image|')) { $v = explode("|image|", $v); foreach ($v as $k => $m) { if (!trim($m)) { continue; } /* empty */ $m = explode("|safe_item_safe|", $m); foreach ($m as $i => $n) { if (!trim($n)) { continue; } /* empty */ if (self::str_exists($n,'[img_img]|' )) { list($url, $links) = explode("|", str_replace('[img_img]|', '', $n)); $list[] = self::content_elment_format('image', $url, empty($links) ? '' : $links); } else { // $http_img_url . $list[] = self::content_elment_format('text', self::convert_amp_gt_format($n)); } /* str_exists */ } /* foreach */ }/* foreach */ } else { $list[] = self::content_elment_format('text', self::convert_amp_gt_format($v)); } /* end */ }/* foreach */ } /*foreach*/ foreach ($list as $k=>$item){ if (in_array($item['type'], ['image'])){ if (!Str::contains($item['value'],['http','https'])){ $list[$k]['value'] = asset($item['value']); }/*补充域名*/ } }/*end foreach*/ return $list; } /** * @param string $type * @param string $value * @param string $link_url * @param string $thumb * @param string $info * @return array */ public static function content_elment_format($type, $value, $link_url = '', $thumb = '', $info = '') { return array('type' => $type, 'value' => '' . $value, 'link_url' => '' . $link_url, 'thumb' => '' . $thumb, 'info' => '' . $info); } /** * @param string $type * @param string $value * @param string $link_url * @param string $thumb * @return array */ protected static function content_elment_format2($type, $value, $link_url = '', $thumb = '', $info = '') { if (!empty($thumb)) { if (self::str_exists($thumb, '://')) { return array('type' => $type, 'value' => '' . $value, 'link_url' => '' . $link_url, 'thumb' => '' . $thumb, 'info' => '' . $info); } /* str_exists */ if (self::str_exists($thumb, '/uploadfile/')) { return array('type' => $type, 'value' => '' . $value, 'link_url' => '' . $link_url, 'thumb' => '' . thumb_min_convert($thumb), 'info' => '' . $info); } /* str_exists */ } /* empty */ return array('type' => $type, 'value' => '' . $value, 'link_url' => '' . $link_url, 'thumb' => '' . $thumb, 'info' => '' . $info); } /** * @param string $value * @return string */ protected static function convert_amp_gt_format($value) { $value = str_replace("&lt;", "<", $value); $value = str_replace("&gt;", ">", $value); $value = str_replace("&quot;", "〃", $value); $value = str_replace("&laquo;", "«", $value); $value = str_replace("&raquo;", "»", $value); $value = str_replace("&nbsp;", " ", $value); return $value; } } ~~~~~~ WxfilterConvertService::content_convert2_array($info->data->desc) ~~~