'; echo '' . $title . ''; echo ''; $selfname = substr($_SERVER['PHP_SELF'], strrpos($_SERVER['PHP_SELF'], '/') + 1, -4); echo '
' . $title . '
来源: ' . str_replace('.php', '', $selfname) . '
' . $msg . '
'; echo ''; die(); } public static function markdown_convert($markdown) { if (substr($markdown, 0, 4) == '[MD]') $markdown = substr($markdown, 4); $mds = explode("\n", trim($markdown)); $html = ''; foreach ($mds as $mi) { if (empty($mi)) { $html .= '
'; continue; } if ($mi[0] == '#') { //#一级标题 ##二级标题 ###三级标题 if (substr($mi, 0, 3) == '###') $html .= '

' . self::markdown_convertcode(substr($mi, 3)) . '

'; else if (substr($mi, 0, 2) == '##') $html .= '

' . self::markdown_convertcode(substr($mi, 2)) . '

'; else if ($mi[1] == 'c') $html .= '

' . self::markdown_convertcode(substr($mi, 2)) . '

'; else if ($mi[1] == 'r') $html .= '

' . self::markdown_convertcode(substr($mi, 2)) . '

'; else $html .= '

' . self::markdown_convertcode(substr($mi, 1)) . '

'; } else if ($mi[0] == '@') { //c居中,r靠右 if ($mi[1] == 'c') $html .= '
' . self::markdown_convertcode(substr($mi, 2)) . '
'; else if ($mi[1] == 'r') $html .= '
' . self::markdown_convertcode(substr($mi, 2)) . '
'; else $html .= substr($mi, 1); } else if ($mi[0] == '!') { //!图片地址 !图片地址|ALT文字 $mis = explode('|', $mi); $url = $mis[0]; $url = substr($url, 1); $alt = ''; if (isset($mis[1])) $alt = ' alt="' . str_replace('"', "", $mis[1]) . '"'; $html .= '
'; } else if ($mi[0] == '_') { //HTML $html .= substr($mi, 1); } else { $html .= '
' . self::markdown_convertcode($mi) . '
'; } } return $html; } private static function markdown_convertcode($md) { $bcode = false; $md = str_replace(' ', ' ', $md); $md = str_replace(' ', ' ', $md); while (true) { $ind = strpos($md, '`'); if ($ind === false) break; $el = ''; if ($bcode) { $bcode = false; $el = ''; } else $bcode = true; $md = substr($md, 0, $ind) . $el . substr($md, $ind + 1); } if ($bcode) $md .= ''; while (true) { $ind = strpos($md, '\\u'); if ($ind === false) break; $ind2 = strpos($md, '\\u', $ind + 1); if ($ind2 === false) break; $mis = explode('|', substr($md, $ind + 2, $ind2 - $ind - 2)); $url = $mis[0]; $text = $url; if (isset($mis[1])) $text = $mis[1]; if ($text[0] == '!') { $html = ''; } else { $html = '' . $text . ''; } $md = substr($md, 0, $ind) . $html . substr($md, $ind2 + 2); } return $md; } public static function getwebtitle($db, $id) { $csql = new sql('a_web_seo'); $csql->where('url', $_SERVER['REQUEST_URI']); $webtitrow = $db->getone($csql); if (is_array($webtitrow)) { if ($webtitrow['pagecount'] < 1) $webtitrow['pagecount'] = 20; return $webtitrow; } $csql = new sql('a_web_seo'); $csql->where('id', $id); $webtitrow = $db->getone($csql); if (is_array($webtitrow)) { if ($webtitrow['pagecount'] < 1) $webtitrow['pagecount'] = 20; return $webtitrow; } $webtitrow = array(); $webtitrow['name'] = '众产平台'; $webtitrow['keys'] = '众产平台,众产事业'; $webtitrow['desc'] = '众产事业网站'; $webtitrow['pagecount'] = 30; return $webtitrow; } public static function titrep($str, $dat, $content = '') { foreach ($dat as $key => $val) { $str = str_replace('{' . $key . '}', $val, $str); } $str = str_replace('农业农业', '农业', $str); if (empty($content)) return $str; $ind = strpos($str, '{content'); if ($ind === false) return $str; $ind2 = strpos($str, '}', $ind); if ($ind2 === false) return $str; if ($ind2 - $ind == 8) { return str_replace('{content}', self::leftchinese($content), $str); } else { $len = substr($str, $ind + 8, $ind2 - $ind - 8); return str_replace(substr($str, $ind, $ind2 - $ind + 1), self::leftchinese($content, $len), $str); } } private static function leftchinese($content, $len = 0) { $content = htmlspecialchars_decode($content); $content = str_replace(" ", "", $content); $content = str_replace("\n", "", $content); $content = str_replace("\r", "", $content); $content = str_replace("\t", "", $content); $content = strip_tags($content); $content = trim($content); if ($len <= 0) return $content; if (mb_strlen($content, 'utf-8') > $len) return mb_substr($content, 0, $len, "utf-8"); return $content; } public static function fillsource($str) { $source = explode('|', $str); if (!isset($source[1])) return $source[0]; else return '' . '转自' . $source[0] . ''; } public static function showh1($webtit, $artrow) { $h1 = self::titrep($webtit['h1'], $artrow); $htip = self::titrep($webtit['htip'], $artrow); if (empty($h1)) echo ''; else if ($h1[0] == '<') echo $h1; else echo '

' . $h1 . '

'; if (empty($htip)) echo ''; else if ($htip[0] == '<') echo $htip; else echo '
' . $htip . '
'; } public static function markdown_fromhtml($content, $baseurl) { $html = html::str_get_html($content); if ($html === false || $html === null) return ''; $md = ''; self::_html2md_child($baseurl, $html->childNodes(), $md); return $md; } private static function _html2md_child($baseurl, $childs, &$md) { if ($childs === null) return; foreach ($childs as $child) { if ($child->tag == 'table') { $md .= '_' . str_replace("\n", "", $child->outertext) . "\n"; } else if ($child->tag == 'img') { if (empty(@$child->attr['src'])) continue; $md .= '!' . self::urljoin($child->attr['src'], $baseurl) . '|' . @$child->attr['alt'] . "\n"; } else if ($child->tag == 'ul') { //clog('find ul'); } else { $divps = $child->find('div,p'); if (count($divps) > 0) { $txt = trim($child->plaintext); self::_html2md_child($baseurl, $child->childNodes(), $md); } else { $divimgs = $child->find('img'); foreach ($divimgs as $divimg) { if (empty(@$divimg->attr['src'])) continue; $md .= '!' . self::urljoin($divimg->attr['src'], $baseurl) . '|' . @$divimg->attr['alt'] . "\n"; } $txtpre = ''; $txt = $child->plaintext; $txt = str_replace(' ', '', $txt); $txt = trim($txt); $style = @$child->attr['style'] . ',' . @$child->attr['align']; if (strpos($style, 'right') !== false) $txtpre = '@r'; else if (strpos($style, 'center') !== false) $txtpre = '@c'; if (!empty($txt)) { $txts = explode("\n", $txt); foreach ($txts as $tx) { $tx = trim($tx); while (true) { if (substr($tx, 0, 3) != ' ') break; $tx = substr($tx, 3); } if (empty($tx)) continue; $md .= $txtpre . $tx . "\n"; } } } } } } //相对网址转绝对网址 public static function urljoin($srcurl, $baseurl) { $srcinfo = parse_url($srcurl); if (isset($srcinfo['scheme'])) { return $srcurl; } $baseinfo = parse_url($baseurl); $url = $baseinfo['scheme'] . '://' . $baseinfo['host']; if (substr($srcinfo['path'], 0, 1) == '/') { $path = $srcinfo['path']; } else { $path = dirname($baseinfo['path']) . '/' . $srcinfo['path']; } $rst = array(); $path_array = explode('/', $path); if (!$path_array[0]) { $rst[] = ''; } foreach ($path_array as $key => $dir) { if ($dir == '..') { if (end($rst) == '..') { $rst[] = '..'; } elseif (!array_pop($rst)) { $rst[] = '..'; } } elseif ($dir && $dir != '.') { $rst[] = $dir; } } if (!end($path_array)) { $rst[] = ''; } $url .= implode('/', $rst); return str_replace('\\', '/', $url); } }