我有一个 div,我在其中显示来自数据库的格式化文本,当我想在 div 中显示的字符数用完时,也会出现分页。但是,有时也会显示 html 标记。我想我必须检测 html 标签并在最大字符用完时使它们不会中断。如何解决这个问题?
我使用http://coursesweb.net/php-mysql/pagination-class-script_s2的一部分对内容进行分页
// method to split the text into number of characters (specified in the $txtchr property) without breaking words
public function getText($text) {
// if the $txtpieces higher than 0, divide the $text into a number of pieces specified in $txtpieces
// otherwise, split the text according to the number of characters specified in $txtchr property
if($this->txtpieces>0) {
$this->txtchr = ceil(strlen($text) / $this->txtpieces); // set $txtchr (length of text piece) according to number of pieces
}
// split the text and create an Array with string pieces. Returns the content for the current page
$newtext = wordwrap($text, $this->txtchr, '#|#');
$ar_text = explode('#|#', $newtext);
$nr_pieces = count($ar_text);
// if paginate by number of pieces, and too many pieces - merge the last two pieces
if($this->txtpieces>0 && $nr_pieces>$this->txtpieces) {
$ar_text[$nr_pieces-2] .= ' '. $ar_text[$nr_pieces-1];
unset($ar_text[$nr_pieces-1]);
}
$this->totalpages = count($ar_text); // sets the number of total pages
if($this->idpage > $this->totalpages) $this->idpage = $this->totalpages;
// sets a string to be added at the end of the text content, if not the last page.
$end = ($this->idpage+1)<$this->totalpages ? ' ...[<i> Continue to next page</i>].' : '';
return $ar_text[$this->idpage]. $end;
}
// method that sets the links
public function getLinks() {
$re_links = ''; // the variable that will contein the links and will be returned
$currentpage = $this->idpage + 1; // because the page index starts from 0, adds 1 to set the current page
$pag_get = '?pg='; // the name for the GET value added in URL
// if $totalpages>0 and totalpages higher or equal to $currentpage
if($this->totalpages>0 && $this->totalpages >= $currentpage) {
// linksto first and previous page, if it isn't the first page
if ($currentpage > 1) {
// show << for link to 1st page
$re_links .= ' <a href="'. $this->pag. '" title="Link 1">First <<</a> ';
$prevpage = $currentpage - 1; // the number of the previous page
// show < for link to previous page, if higher then 1
if($prevpage>1) $re_links .= ' <a href="'. $this->pag. $pag_get. $prevpage. '" title="Link '. $prevpage. '">Previous <</a> ';
}
// sets the links in the range of the current page
for($x = ($currentpage - $this->range); $x <= ($currentpage + $this->range); $x++) {
// if it's a number between 0 and last page
if (($x > 0) && ($x <= $this->totalpages)) {
// if it's the number of current page, show the number without link, otherwise add link
if ($x == $currentpage) $re_links .= ' [<b>'. $x. '</b>] ';
else $re_links .= ' <a href="'. $this->pag. $pag_get. $x. '" title="Link '. $x. '">'. $x. '</a> ';
}
}
// If the current page is not final, adds link to next and last page
if ($currentpage != $this->totalpages) {
$nextpage = $currentpage + 1;
// show > for next page (if higher then $this->range and less then totalpages)
if($nextpage>$this->range && $nextpage<$this->totalpages) $re_links .= ' <a href="'. $this->pag. $pag_get. $nextpage. '" title="Link '. $nextpage. '">> Next</a> ';
// show >> for last page, if higher than $this->range
if($this->totalpages>$this->range) $re_links .= ' <a href="'. $this->pag. $pag_get. $this->totalpages. '" title="Link '. $this->totalpages. '">>> Last ('. $this->totalpages. ')</a> ';
}
}
// adds all links into a DIV and return it
if(strlen($re_links)>1) $re_links = '<div class="linkspg">'. $re_links. '</div>';
return $re_links;
}
在我的 php 文件中
// create object instance of the class
$objPg = new Pagination();
// change the number of characters for the piece of text content
$objPg->txtchr = 600;
在我的html里面
<div id="left2"><?php echo "<center><b><i>$story_title</b></i> Chapter $chapter_no<b><i> $chapter_title</i></b></center>";?>
<div id = "ellipsis"><?php // output /display the conntent
echo $objPg->getText($chapter_content);
// output / display pagination links
echo $objPg->getLinks($story_id); ?></div>
</div>
我得到了第一页的输出
This is 6 (24) pt. No other format.
**This is 6 (24) pt. In bold.**
*This is 6 (24) pt. In italic format.*
This is 6 (24) pt. In underline manner.
Continue to next page].
[1] 2
但是当我单击 [2] 显示下一个文本时,我得到了这个
style="font-size: x-large;">***This is 7 (36) pt. In bold, italic and underlined format.***
First << 1 [2]
我已经尝试过 html_entity_decode 但结果是一样的,我该怎么办?
提前致谢 :)