public static function getAbcSortArr($paramArr) { $options = array('data' => ''); if (is_array($paramArr)) { $options = array_merge($options, $paramArr); } extract($options); if (!is_array($data)) { $data = explode(',', $data); } $classArr = array(); if ($data) { foreach ($data as $key => $value) { //进行字母索引 $f_letter = API_Item_Base_String::getFirstLetter(array('input' => $value)); $akey = ord($f_letter); $classArr[$key] = array("val" => $value, "letter" => $f_letter); } } return $classArr; }
/** * 获得Dom操作对象 */ public static function getHtmlOrDom($paramArr) { $options = array('url' => 'http://www.baidu.com/', 'charset' => 'utf-8', 'timeout' => 3, 'isGearman' => false, 'getDom' => false, 'snoopy' => false, 'fileGetContents' => false, 'ungzip' => false, 'referer' => false, 'proxy' => false, 'maxredirs' => 6, 'header' => false); if (is_array($paramArr)) { $options = array_merge($options, $paramArr); } extract($options); #获得页面内容 if ($isGearman) { $htmlStr = API_Gearman::doNormal(array('taskName' => 'adsl_get_contents', 'taskContent' => $url)); } else { if ($snoopy) { $htmlStr = self::snoopyFetch(array('url' => $url, 'referer' => $referer, 'proxy' => $proxy, 'maxredirs' => $maxredirs, 'header' => $header)); } elseif ($fileGetContents) { $htmlStr = file_get_contents($url); } else { $htmlStr = API_Http::curlPage(array('url' => $url, 'timeout' => $timeout)); } } if (!$htmlStr) { return false; } if ($ungzip) { $htmlStr = API_Item_Base_String::gzdecode(array('input' => $htmlStr)); } if (!$getDom) { return $htmlStr; } #载入处理类 require_once LJL_API_ROOT . '/Libs/FetchHtml/SimpleHtmlDom.php'; if ($charset == "UTF-8" || $charset == "utf-8") { $htmlStr = mb_convert_encoding($htmlStr, "GBK", "utf-8"); } $dom = new simple_html_dom(null, $lowercase = true, $forceTagsClosed = true, 'GBK'); //还是用gbk去解析 $dom->load($htmlStr, $lowercase = true, $stripRN = true); return $dom; }
/** * 发布文章 * @param LJL_Request $input * @param LJL_Response $output */ public function doPublish(LJL_Request $input, LJL_Response $output) { $isPublish = intval($input->post('ispublish')); //0:预览,1:发布 $articleId = intval($input->post('articleId')); //0:插入,否则:修改 $cate = $input->post('cate'); $title = $input->post('title'); $source = $input->post('source'); $tags = $input->post('tags'); $content = htmlspecialchars(addslashes($input->post('content', 1))); $imgArr = $input->post('imgArr', 1); //首图id $firstImgUrl = $this->matchFirstPic($input->post('content', 1)); $firstImgName = $this->getFirtstPicName($firstImgUrl); $firstImgId = Helper_Blog::getPicId(array('picName' => $firstImgName)); //有无首图截取的是不一样的 $desc = $firstImgId ? API_Item_Base_String::getShort(array('str' => $input->post('content'), 'length' => 120)) : API_Item_Base_String::getShort(array('str' => $input->post('content'), 'length' => 125)); //数据集 $insertData = array('isPublished' => $isPublish, 'firstImgId' => $firstImgId, 'cate' => $cate, 'title' => $title, 'descript' => $desc, 'source' => $source, 'tags' => $tags, 'content' => $content, 'imgArr' => $imgArr, 'updateTime' => SYSTEM_TIME); //update if ($articleId) { $insertData['updateTime'] = SYSTEM_TIME; Helper_Blog::updateArticleInfo(array('articleId' => $articleId, 'updateData' => $insertData)); //insert } else { //先查看下数据库中是否已经有该文章的题目了(分类也要一致才是) if (Helper_Blog::ishasArticle($cate, $title)) { echo 'error'; die; } $insertData['insertTime'] = SYSTEM_TIME; $articleId = Helper_Blog::insertArticleInfo(array('insertData' => $insertData)); } echo $articleId; die; }
/** * 记录搜索 */ public static function searchRecord($webSite, $keyword = '') { if (!$webSite || !$keyword || $keyword === '*') { return; } $db = Db_Blogconfig::instance(); //先查询有没有改记录,有改记录则加1 $sql = " select id from blog_search_words where webSite='{$webSite}' and keyword='{$keyword}'"; $searchId = $db->getOne($sql); $pinyin = API_Item_Base_String::getPinyin(array('input' => $keyword)); $insertData = array('webSite' => $webSite, 'keyword' => $keyword, 'pinyin' => $pinyin); //有则记录加1 if ($searchId) { $sql = "update blog_search_words set times=times+1 where id={$searchId} "; } else { //没有记录则插入 $formatData = self::setInsertCondition($insertData); $sql = "insert into blog_search_words({$formatData['fileds']}) values({$formatData['values']})"; } $db->query($sql); }
/** * 产品线选择 */ public function doSelectSubcate(ZOL_Request $input, ZOL_Response $output) { $manuId = (int) $input->get('manuId'); $subcateIdStr = $input->get('subcateIdStr'); if ($subcateIdStr) { $subcateIdArr = explode(',', $subcateIdStr); } #取得所有品牌数据 // $dataArr = ZOL_Api::run("Pro.Cate.getSubcateByDb" , array( // 'manuId' => $manuId, #品牌ID // 'noSecond'=> 1 // )); #数据源数组配置 $dataArr = Helper_Pro_Pro::getSubcateTemp(array()); if ($dataArr) { foreach ($dataArr as $key => $val) { if (isset($subcateIdArr) && in_array($val['subcateId'], $subcateIdArr)) { continue; } $manuName = ZOL_String::trimWhitespace($val['name']); #获得首字母 $tfl = API_Item_Base_String::getFirstLetter(array('input' => $manuName)); if (!isset($outArr[$tfl])) { $outArr[$tfl] = array('name' => $tfl, 'cons' => array()); } $outArr[$tfl]['cons'][] = $val; } sort($outArr); $output->outArr = $outArr; } $output->setTemplate('Ajax/SelectSubcate'); }
?> " target="_blank" title="<?php echo $articleInfo['cateVal']; ?> ">[<?php echo $articleInfo['cateVal']; ?> ]</a> <a href="<?php echo str_replace(APP_BLOG_NAME, $articleInfo['webSite'], Blog_Plugin_Urls::getDetailUrl(array('articleid' => $articleInfo['articleId']))); ?> " target="_blank" title="<?php echo $articleInfo['title']; ?> " class="title"><?php echo API_Item_Base_String::getShort(array('str' => $articleInfo['title'], 'length' => 12)); ?> ...</a><span><?php echo date('m-d', $articleInfo['publishTime']); ?> </span> </li> <?php } } ?> </ul> </div> <div style="clear:both"></div> </div> </div>
/** * 插入文章 */ private function insertArticle($url, $title, $content) { if (!$title || !$content) { return; } $db = Db_Blog::instance(null, $this->website); //已有该文章则退出 $title = htmlspecialchars(strip_tags(addslashes(str_replace("'", '"', $title)))); if (Helper_Blog::ishasArticle('', $title)) { return; } //echo $title;die; //对于有图片的文章进行搬运图片处理 preg_match_all("/<img src=\"(.*?)\"/", $content, $matches, PREG_SET_ORDER); $imgidArr = array(); if ($matches) { if (count($matches) > 10) { return; } foreach ((array) $matches as $imgsrc) { $imgInfo = $this->getImgNameDir(); if (!$imgsrc[1]) { continue; } //1、移动图片 exec('wget -q --tries=3 -O ' . rtrim($imgInfo[1], '/') . '/' . $imgInfo[0] . '.jpg ' . $imgsrc[1]); //2、向自己库中插入图片 $imgidArr[] = Helper_Blog::insertPic(array('picName' => $imgInfo[0], 'picExt' => 'jpg', 'time' => $imgInfo[3])); //3、图片地址字符串替换 $content = str_replace($imgsrc[1], $imgInfo[2] . '/' . $imgInfo[0] . '.jpg', $content); } } //插入文章 $firstImgId = $imgidArr ? $imgidArr[0] : 0; $desc = $firstImgId ? API_Item_Base_String::getShort(array('str' => strip_tags($content), 'length' => 120)) : API_Item_Base_String::getShort(array('str' => strip_tags($content), 'length' => 150)); $articleId = Helper_Blog::insertArticleInfo(array('insertData' => array('firstImgId' => $firstImgId, 'insertTime' => SYSTEM_TIME, 'isPublished' => 0, 'cate' => $this->cate, 'tags' => $this->tags, 'title' => $title, 'descript' => ' ' . strip_tags(addslashes(str_replace("'", '"', $desc))) . ' ', 'source' => $url, 'content' => ' ' . htmlspecialchars(addslashes(preg_replace("/<script[\\s]*.*?<\\/script>/si", '', $content))) . ' ', 'imgArr' => $imgidArr))); echo 'article ' . $articleId . ' is ok' . PHP_EOL; }
/** * 文本相似判断 */ public static function simText($paramArr) { $options = array('str1' => "", 'str2' => "", 'lcs' => 0, 'diff' => 0); if (is_array($paramArr)) { $options = array_merge($options, $paramArr); } extract($options); $str1 = stripslashes($str1); $str2 = stripslashes($str2); $str1 = API_Item_Base_String::splitStr(array('input' => $str1)); $str2 = API_Item_Base_String::splitStr(array('input' => $str2)); $len1 = count($str1); $len2 = count($str2); array_unshift($str1, ''); array_unshift($str2, ''); $C = $L = array(); $res = ''; #动态规划算法自底向上地计算过程 for ($j = 0; $j <= $len1; $j++) { $C[0][$j] = 0; } for ($i = 0; $i <= $len2; $i++) { $C[$i][0] = 0; } for ($i = 1; $i <= $len2; $i++) { for ($j = 1; $j <= $len1; $j++) { if ($str1[$j] == $str2[$i]) { $C[$i][$j] = $C[$i - 1][$j - 1] + 1; $L[$i][$j] = '`'; } else { if ($C[$i - 1][$j] >= $C[$i][$j - 1]) { $C[$i][$j] = $C[$i - 1][$j]; $L[$i][$j] = '↑'; } else { $C[$i][$j] = $C[$i][$j - 1]; $L[$i][$j] = '←'; } } } } #返回最长公共子序列 $i = $len2; $j = $len1; $commonArr = array(); $orgin['str1'] = $orgin['str2'] = array(); while ($i > 0 && $j > 0) { $direct = $L[$i][$j]; if ($direct == '`') { array_unshift($commonArr, $str1[$j]); $orgin['str1'][] = $j; $orgin['str2'][] = $i; $i--; $j--; } if ($direct == '↑') { $i--; } if ($direct == '←') { $j--; } } #用Tanimoto系数计算相似度 $Nc = count($commonArr); $Na = $len1; $Nb = $len2; $sim = 0; if ($Na + $Nb - $Nc > 0) { $sim = $Nc / ($Na + $Nb - $Nc); } $sim = sprintf("%.3f", $sim); $res = array('sim' => $sim); if ($lcs) { $res['lcs'] = implode('', $commonArr); } #加颜色,绿色为公共,红色为新文本 if ($diff) { $result['str1'] = self::markTextColor(array('strArr' => $str1, 'orgin' => $orgin['str1'])); $result['str2'] = self::markTextColor(array('strArr' => $str2, 'orgin' => $orgin['str2'])); $res['result'] = $result; } return $res; }
/** * 将数组装换的首字母分装的数组 */ public static function transFirstLetterArr($paramArr) { $options = array('data' => array(), 'type' => 1, 'sel' => false); if (is_array($paramArr)) { $options = array_merge($options, $paramArr); } extract($options); $outArr = array(); if ($data) { if ($type == 1) { #1:默认select 形式 return api_json_encode($data); } elseif ($type == 3) { #原生态select的option $out = ''; foreach ($data as $k => $wd) { $seled = $sel == $k ? "selected" : ""; $out .= "<option value='{$k}' {$seled}>{$wd}</option>"; } return mb_convert_encoding($out, "UTF-8", "GBK"); } elseif ($type == 2) { #div select 形式 foreach ($data as $k => $wd) { $l = API_Item_Base_String::getFirstLetter(array('input' => $wd)); #获得首字母 if (!isset($outArr[$l])) { $outArr[$l] = array('name' => $l, 'cons' => array()); } $outArr[$l]['cons'][] = $wd; } //var_dump($outArr); sort($outArr); return api_json_encode($outArr); } elseif ($type == 4) { #原生态select,按照字母排序的 foreach ($data as $k => $wd) { $l = API_Item_Base_String::getFirstLetter(array('input' => $wd)); #获得首字母 $outArr[$k] = $l . "_" . $wd; } asort($outArr); $out = ''; foreach ($outArr as $k => $wd) { $seled = $sel == $k ? "selected" : ""; $out .= "<option value='{$k}' {$seled}>{$wd}</option>"; } return $out; } } return ''; }