文章字数

This commit is contained in:
rainerosion 2022-02-05 16:58:32 +08:00
parent 3ee7cb0ee6
commit b285084411
2 changed files with 32 additions and 15 deletions

View File

@ -7,45 +7,47 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit;
* @param $obj
* @return 获取文章头图
*/
function get_postthumb($obj) {
function get_postthumb($obj)
{
if(isset($obj->fields->thumb)){
if (isset($obj->fields->thumb)) {
return $obj->fields->thumb;
}
preg_match( "/<img[^>]+src\s*=\s*['\"]([^'\"]+)['\"][^>]*>/ ", $obj->content, $matches );
if(isset($matches[1])){
preg_match("/<img[^>]+src\s*=\s*['\"]([^'\"]+)['\"][^>]*>/ ", $obj->content, $matches);
if (isset($matches[1])) {
return $matches[1];
}
preg_match( "/!\[(.*?)\]\((.*?)\)/ ", $obj->content, $matches );
if(isset($matches[2])) {
return$matches[2];
preg_match("/!\[(.*?)\]\((.*?)\)/ ", $obj->content, $matches);
if (isset($matches[2])) {
return $matches[2];
}
$thumbPath = __DIR__ .'/static/medias/featureimages/';
$thumbPath = __DIR__ . '/static/medias/featureimages/';
$files = scandir($thumbPath);
$thumbs = [];
for ($i = 0; $i < count($files); $i++) {
if(is_file($thumbPath . $files[$i])){
if (is_file($thumbPath . $files[$i])) {
$thumbs[] = $files[$i];
}
}
return Helper::options()->themeUrl .'/static/medias/featureimages/'. $thumbs[rand(0, count($thumbs) - 1)];
return Helper::options()->themeUrl . '/static/medias/featureimages/' . $thumbs[rand(0, count($thumbs) - 1)];
}
/**
* @param $obj 对象
* @return string 上一页url
*/
function getPrevPageLink($obj){
function getPrevPageLink($obj)
{
ob_start();
$obj->pagelink("prev", "prev");
$pageLink = ob_get_contents();
ob_end_clean();
preg_match("/<a[^>]+href\s*=\s*['\"]([^'\"]+)['\"][^>]*>/", $pageLink, $matches);
if(isset($matches[1])){
if (isset($matches[1])) {
return $matches[1];
}
return "#";
@ -55,14 +57,28 @@ function getPrevPageLink($obj){
* @param $obj 对象
* @return string 下一页url
*/
function getNextPageLink($obj){
function getNextPageLink($obj)
{
ob_start();
$obj->pageLink("next", "next");
$pageLink = ob_get_contents();
ob_end_clean();
preg_match("/<a[^>]+href\s*=\s*['\"]([^'\"]+)['\"][^>]*>/", $pageLink, $matches);
if(isset($matches[1])){
if (isset($matches[1])) {
return $matches[1];
}
return "#";
}
/**
* 统计文章字数
*
* @param $cid
*/
function articleCount($cid)
{
$db = Typecho_Db::get();
$rs = $db->fetchRow($db->select('table.contents.text')->from('table.contents')->where('table.contents.cid=?', $cid)->order('table.contents.cid', Typecho_Db::SORT_ASC)->limit(1));
$text = preg_replace("/[^\x{4e00}-\x{9fa5}]/u", "", $rs['text']);
return mb_strlen($text, 'UTF-8');
}

View File

@ -40,7 +40,8 @@ $this->need('base/article.style.php');
<?php _e(date('Y-m-d', $this->modified)) ;?>
</div>
<div class="info-break-policy"><i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
75
<!-- --><?// echo $this->cid;?>
<?php _e(articleCount($this->cid));?>
</div>
<div class="info-break-policy"><i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp; 1
</div>