3
votes

I have a function that truncates texts as follows:

public function truncateText($text, $val)
    {
        if(strlen($text) > $val){
            $content = mb_substr($text, 0, $val) . '...';
            return $content;
        } else {
            return $text;
        }
    }

I am having issues with multibyte characters. Even though i am using mb_substring i am still getting strange characters at the end of the text. My mb_internal_encoding is UTF-8.

An exemple would be as follows:

The string stored in the database is :

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent malesuada pretium justo, non posuere enim semper vel. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aliquam iaculis nulla velit, eget accumááááá.</p>

When i run this string with the truncateText i get the following with and amp letter at the end:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent malesuada pretium justo, non posuere enim semper vel. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aliquam iaculis nulla velit, eget accum&...

$this->tag->truncateText($text, 250);

However if i test it with the text directly without fetching it from the database and without the

tags then the truncate is good. I tried strip_tags in the string got from the database but still nothing.

var_dump of the mentioned string that is stored in the database:

string(925) "

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent malesuada pretium justo, non posuere enim semper vel. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aliquam iaculis nulla velit, eget accumááááá.

"

var_dump of htmlspecialcharacters:

string(949) "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent malesuada pretium justo, non posuere enim semper vel. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aliquam iaculis nulla velit, eget accum&aacute;&aacute;&aacute.</p> "

What am i missing here?

Many thanks, Trix

2
can u post a var_dump of this string right after it was fetched from the DB? Before you truncate it?sietse85
Sounds like you did something wrong, like storing HTML entities in your database.misorude
Updatet post with a var_dump.trix87
the weird thing is his code works fine on the php-cli tested it without any problem with his string and his functionsietse85
Check the output of that var_dump in the source code view in your browser, not after your browser has already interpreted it as HTML. (Or use echo htmlspecialchars($value) instead to make a debug output.)misorude

2 Answers

1
votes

In your database adapter configuration add 'options':

use Phalcon\Db\Adapter\Pdo\Mysql;

$db = new Mysql(
     /* ... */
     'options'  => [
        PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"
      ]
   );  
0
votes

Managed to resolve the problem. I rewrote my function a little bit, now its oke.

public function truncateText($text, $val)
{
    if(mb_strlen($text) > $val){
        $content = mb_substr(html_entity_decode(str_ireplace(['<p>','</p>'],'',$text)), 0, $val) . '...';
        return $content;
    } else {
        return $text;
    }
}

Thanks for the advices and help, really appreaciate it. Trix