Yii 1.1: ereadmore

Yii framework simple extension to shorten an html code.


A simple extension to shorten an html code. - version 1.0 - author Dimitrios Mengidis tydeas.dr@gmail.com



This a common functionality found in blogs, where you see a part of an article and there is link usually named read more where get redirected to the full article. As an example you can check my blog.


Simple case

In the view add:

<?php $this->beginWidget('application.extensions.EReadMore.EReadMore', array(
        <p>A simple extension to shorten an html code.</p>
              <li><a href='php.net/manual/en/intro.dom.php' >DOMDocument</a></li>
     <?php $this->endWidget(); ?>

Advanced use

Check the EReadMore code options to see what you can set.


Total 5 comments

#16418 report it
mocapapa at 2014/02/20 09:58pm
Fixed version of EReadMore

I found several problems in this widget, but it is no longer maintained unfortunately. I have hacked a couple of hours to solve several issues as follows.

1.DOCTYPE issue
There is the same discussion on the forum, I copied the idea to the following function.

function fragmentHtml($html) {
        return preg_replace('/^<!DOCTYPE.+?>/', '', str_replace(array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $html));

How to use is as follows.

$this->short = $this->fragmentHtml($shortdom->saveHTML());

2.UTF-8 issue
It does not handle UTF-8 based text well. There also is a solution on the forum as follows.

$this->doc->loadHTML("<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"></head>".$html);

3.Chopping the last of the sentence
I prefer to separate the sentence at the end. Then I implemented the idea of the separator as follows. This can be more generic as the separator is hard coded, sorry. And we will remove this tag at last.

public function run()
        $html=str_replace('', '。<hr class="period">', ob_get_clean());

4.Counting character numbers
If the number of the characters exceeds the maximum, then the widget will produce the link of readmore.

$newnode = $shortdom->importNode($node, true);
               $count += strlen($newnode->textContent);
               if ($count < $this->maxChar) {
               } else {
                  $overFlag = true;

5.Removing specific <hr> and more
We have to remove inserted <hr> and <p></p> that is inserted by the system.

function fragmentHtml($html) {
        return preg_replace('/^<!DOCTYPE.+?>/', '', str_replace(array('<html>', '</html>', '<body>', '</body>', '<hr class="period">'), array('', '', '', '', ''), $html));
$this->doc->loadHTML("<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"></head><hr class=\"period\">".$html);

After those modification, the widget is working perfectly. Thanks go to the author for the great idea of using DOMDocument. Hope this helps.

#11538 report it
code4d1 at 2013/01/19 06:51am
read more..

for urgent solution to meet my need I make a read more with very simple php code strip_tags and substr

$desc = strip_tags(substr($data->description, 0, 200))."....";        
echo $desc;
echo CHtml::link('read more', array('view', 'id'=>$data->id),array('class'=>'btn btn-success btn-small'));

demo : www (dot) lowongankerja (dot) dewatatech (dot) com

#11378 report it
WebDevPT at 2013/01/10 05:12am

I manage to use this the "old fashion" way by using this code instead

$string = $data->content;
if(mb_strlen($string, 'utf-8') >= 700){
       $string = mb_substr($string, 0, 700 - 5, 'utf-8')." ".CHtml::link(CHtml::encode("read more..."), $data->url);
echo $string;

utf works ok and there's no need to use extensions. But still there's the problem with the cuts inside some tags like "ul". But it works.

#11377 report it
WebDevPT at 2013/01/10 04:12am
Utf problem

For this to work for a certain number of chars I'm using this, but still can't work around utf, any ideas?

if (strlen($data->content)<500) {
                              echo $data->content;
                            if (strlen($data->content)>500) {
                                $this->beginWidget('application.extensions.ereadmore.EReadMore', array('linkUrl'=>$data->url));
                                echo $data->content;
                            else {
                              echo $data->content;
                            //echo $data->content;
#11376 report it
WebDevPT at 2013/01/10 03:42am
Utf characters

After a few more tests i found out that this is actually working.

$this->beginWidget('application.extensions.ereadmore.EReadMore', array('linkUrl'=>$data->url));
    echo $data->content;

but the utf characters are not properly shown. Any ideas?

Leave a comment

Please to leave your comment.

Create extension