php html 字符串 转 数组 用于小程序转换

php html 字符串 转 数组 用于小程序转换

private function htmlToArray($content) //修改后
    {
		//使用正则替换数据
        $content = preg_replace('/

(.*)<\/p>/Uis', '--pgSuperSpliteGraceUI--p::pgSuperSplite::$1--pgSuperSpliteGraceUI--', $content); $content = str_replace(array("\r\n", "\r", "\n"), '', $content); $content = preg_replace('/.*<\/a>/Uis', '--pgSuperSpliteGraceUI--link::pgSuperSplite::$1--pgSuperSpliteGraceUI--', $content); $content = preg_replace('//Uis', '--pgSuperSpliteGraceUI--img::pgSuperSplite::$1--pgSuperSpliteGraceUI--', $content); $content = preg_replace('/(.*)<\/pre>/Uis', '--pgSuperSpliteGraceUI--pre::pgSuperSplite::$1--pgSuperSpliteGraceUI--', $content); $content = preg_replace('/(.*)<\/strong>/Uis', '--pgSuperSpliteGraceUI--strong::pgSuperSplite::$1--pgSuperSpliteGraceUI--', $content); $content = preg_replace('//Uis', PHP_EOL, $content); $content = preg_replace('/

(.*)<\/blockquote>/Uis', '--pgSuperSpliteGraceUI--quote::pgSuperSplite::$1--pgSuperSpliteGraceUI--', $content); $content = preg_replace('//Uis', '--pgSuperSpliteGraceUI--spline::pgSuperSplite::...--pgSuperSpliteGraceUI--', $content); $content = strip_tags($content); $content = preg_replace('/(\t)/Uis', '', $content); $content = preg_replace('/ /Uis', ' ', $content); $content = preg_replace('/&.*;/Uis', '', $content); // dump($content); // 拆分数组 $content = explode('--pgSuperSpliteGraceUI--', $content); //dump($content); // 检查数组并处理空白 // 记录需要删除的项目 $contentArray = array(); foreach ($content as $k => $item) { // 如果是空白内容删除它 $res = str_replace(array("\r\n", "\r", "\n", ''), '', $item); if ($res != '') { //dump($res); // 拆分子项目 $itemArr = explode('::pgSuperSplite::', $item); //dump($itemArr); // 文本 if (count($itemArr) < 2) { $contentArray[] = array('type' => 'txt', 'content' => trim($itemArr[0])); } else { if($itemArr[0]=='p'){ $itemArr[0]='txt'; } $contentArray[] = array('type' => $itemArr[0], 'content' => trim($itemArr[1])); } } } return $contentArray; } private function __htmlToArray($content) //老方法 { $content = preg_replace('/

(.*)<\/p>/Uis', '--pgSuperSpliteGraceUI--p::pgSuperSplite::$1--pgSuperSpliteGraceUI--', $content); $content = str_replace(array("\r\n", "\r", "\n"), '', $content); $content = preg_replace('/.*<\/a>/Uis', '--pgSuperSpliteGraceUI--link::pgSuperSplite::$1--pgSuperSpliteGraceUI--', $content); $content = preg_replace('//Uis', '--pgSuperSpliteGraceUI--img::pgSuperSplite::$1--pgSuperSpliteGraceUI--', $content); $content = preg_replace('/(.*)<\/pre>/Uis', '--pgSuperSpliteGraceUI--pre::pgSuperSplite::$1--pgSuperSpliteGraceUI--', $content); $content = preg_replace('/(.*)<\/strong>/Uis', '--pgSuperSpliteGraceUI--strong::pgSuperSplite::$1--pgSuperSpliteGraceUI--', $content); $content = preg_replace('//Uis', PHP_EOL, $content); $content = preg_replace('/

(.*)<\/blockquote>/Uis', '--pgSuperSpliteGraceUI--quote::pgSuperSplite::$1--pgSuperSpliteGraceUI--', $content); $content = preg_replace('//Uis', '--pgSuperSpliteGraceUI--spline::pgSuperSplite::...--pgSuperSpliteGraceUI--', $content); $content = strip_tags($content); $content = preg_replace('/(\t)/Uis', '', $content); $content = preg_replace('/ /Uis', ' ', $content); $content = preg_replace('/&.*;/Uis', '', $content); $content = explode('--pgSuperSpliteGraceUI--', $content); $contentArray = array(); foreach ($content as $k => $item) { $res = str_replace(array("\r\n", "\r", "\n", ''), '', $item); if ($res != '') { $itemArr = explode('::pgSuperSplite::', $item); if (count($itemArr) < 2) { $contentArray[] = array('type' => 'txt', 'content' => trim($itemArr[0])); } else { $contentArray[] = array('type' => $itemArr[0], 'content' => trim($itemArr[1])); } } } return $contentArray; }

你可能感兴趣的:(uniapp,html,php学习,php,开发语言,安全)