使用PHP的文本摘录
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用PHP的文本摘录相关的知识,希望对你有一定的参考价值。
Create excerpts from text block from your wordpress content and add links page / post from the guid.
/** * usage: * $txt = new($post->post_content, 50, 'Read in full', '<span class="readmore">', '</span>', '<a><span>'); * echo $text->createExcerpt(); * */ class text{ public $text; public $length; public $allowedTags; public $readMoreText; //create internal variables from those passed to the class function __construct($text = '', $length = 50, $readMoreText = 'Read In Full...', $before = '', $after = '', $allowedTags = '<a>') { $this->text = $text; $this->length = $length; $this->readMoreText = $readMoreText; $this->before = $before; $this->after = $after; $this->allowedTags = $allowedTags; } //shorten text to required length public function shortenText() { //explode the string into an array of the length we want { //remove the excess string from the end of the array //create a string from the array //cleaning up //return return $textString . '... '; } else { return $this->text; } } //remove tags and other. public function cleanText($text){ //strip the text of html tags, except those we want //remove [] and their content //return return $strippedText; } //create output public function createExcerpt(){ global $post; //create output $output = ''; $output .= $this->cleanText($this->shortenText()); if($this->readMoreText !== '') { $output .= $this->before; $output .= ' <a href="'. $post->guid .'" title="'. $this->readMoreText . ': '. $post->post_title .'">'. $this->readMoreText . '</a>'; $output .= $this->after; } return $output; } }//end class//
以上是关于使用PHP的文本摘录的主要内容,如果未能解决你的问题,请参考以下文章
PHP 显示具有自定义摘录长度的帖子和自定义在WordPress中阅读更多文本