Elxis CMS Forum

Extensions => Modules => Topic started by: ArXoS on June 13, 2010, 12:10:56

Title: mod_latestnews : limited number of characters from the topic title
Post by: ArXoS on June 13, 2010, 12:10:56
Hello Elxis

I need to edit mod_latestnews (to have a standard output wight) to limit the number of characters from the topic title that shows 
So, i changed the code (last lines)

from :
Code: [Select]
$link = sefRelToAbs( 'index.php?option=com_content&task=view&id='. $row->id . $Itemid );
?>
<li class="latestnews<?php echo $class_sfx?>">
<a href="<?php echo $link?>" class="latestnews<?php echo $class_sfx?>">
<?php echo $row->title?>
</a>
</li>
<?php
}
?>

</ul>

to :
Code: [Select]
   $link = sefRelToAbs( 'index.php?option=com_content&amp;task=view&amp;id='. $row->id . $Itemid );
   if (strlen($row->title) >52) {
      $title = substr($row->title, 0, 52).'...';
   } else {
       $title = $row->title;
   }
   ?>
   <li class="latestnews">
      <a href="<?php echo $link?>" class="latestnews">
         <?php echo $title?></a>
   </li>
   <?php
}
?>

</ul>

Its working great, but at the outlook, when the title is limited the last character is �... (not to all titles)
How can I avoid it ?

(http://img534.imageshack.us/img534/8066/modlatestnews.jpg)

Thanks
(publishing language : Greek)
Title: Re: mod_latestnews : limited number of characters from the topic title
Post by: webgift on June 13, 2010, 13:39:24
If all things goes right ;) You must use the strip_tags (http://php.net/manual/en/function.strip-tags.php) function.
Specifically ,

Replace the line :
$title = substr($row->title, 0, 52).'...';

with this one:
$title= substr(strip_tags($row->title),0,52).'...';

And tell me. ;)
Title: Re: mod_latestnews : limited number of characters from the topic title
Post by: ArXoS on June 13, 2010, 13:50:36
nope ... same results  >:(
Title: Re: mod_latestnews : limited number of characters from the topic title
Post by: speck on June 13, 2010, 14:00:56
The file must be encoded unicode UTF8  (no unicode signature BOM )
Before to save it check the properties ;)
Title: Re: mod_latestnews : limited number of characters from the topic title
Post by: ArXoS on June 13, 2010, 14:07:58
nope ... still same results  :(
Title: Re: mod_latestnews : limited number of characters from the topic title
Post by: speck on June 13, 2010, 14:31:32
can you put the file so i check it??
Title: Re: mod_latestnews : limited number of characters from the topic title
Post by: webgift on June 13, 2010, 14:44:42
There is a technique from that on mod_frondpage.php

Code: (php) [Select]
/**************/
/* LIMIT TEXT */
/**************/
public function limittext($txt, $textlimit) {
    $len = eUTF::utf8_strlen($txt);
    if ($len <= $textlimit) {
        return $txt;
    } else {
        $txt = eUTF::utf8_substr($txt, 0, $textlimit);
        $pos = eUTF::utf8_strrpos($txt,' ');
        if ($pos >0) {
        $txt = eUTF::utf8_substr($txt, 0, $pos);
    if ((($tpos = eUTF::utf8_strrpos($txt,'<')) >  eUTF::utf8_strrpos($txt,'>')) && ($tpos > 0)) {
  $txt = eUTF::utf8_substr($txt, 0, $tpos-1);
  }
}
        return $txt.' ...';
    }
}


;)
Title: Re: mod_latestnews : limited number of characters from the topic title
Post by: ArXoS on June 13, 2010, 16:47:46
ok, i think i got it.

(http://img153.imageshack.us/img153/9254/image1as.jpg)

i also added an option to admin panel to set the character limit
(http://img197.imageshack.us/img197/9851/image3bm.jpg)

Its working, i hope it's correct



[attachment deleted by admin]
Title: Re: mod_latestnews : limited number of characters from the topic title
Post by: ArXoS on June 13, 2010, 17:46:59
as i can see, the character length is not the one i set to admin panel. I set 60 characters length and the module returns only near the half. I guess this is because i use the command strlen instead of eUTF::utf8_strlen (as used in mod_frondpage.php)

Can someone help me finish the module with eUTF::utf8_strlen ?

Thanks
Title: Re: mod_latestnews : limited number of characters from the topic title
Post by: speck on June 13, 2010, 18:04:13
i did some modifies and works fine.
test it!


[attachment deleted by admin]
Title: Re: mod_latestnews : limited number of characters from the topic title
Post by: datahell on June 13, 2010, 18:21:30
In Elxis we use the eUTF class to handle multi-byte strings as PHP lacks of unicode support (it will be supported in PHP 6).

Instead of: substr, use eUTF::utf8_substr
Instead of: strlen, use eUTF::utf8_strlen
All the supported methods: includes/Core/utf8.class.php

As the eUTF methods are in general slow use them only when the string may contain unicode characters.
Title: Re: mod_latestnews : limited number of characters from the topic title
Post by: ArXoS on June 13, 2010, 18:33:09
thank you elxis, the module works great. Thanks to the changes of datahell., i can set exactly the right limit with no mistakes ..

Spec, thank you too for your help. I tested your module, and it's also does the job. I placed an else parameter, because all the titles returned with dots
Thanks once more  :D
Title: Re: mod_latestnews : limited number of characters from the topic title
Post by: datahell on June 13, 2010, 20:44:25
Glad to solve this issue Arxos. Elxis has excellent unicode support many years now. By just searching Elxis core files you will find the answers to your problems as such tools are built-in.

Here is an example on how I limit strings:

$text = 'Sample text that we want to limit it down to 30 characters';
$text2 = (eUTF::utf8_strlen($text) > 30) ? eUTF::utf8_substr($text, 0, 27).'...' : $text;

$text2 has a maximum length of 30 characters (including 3 dots at the end).