Search code examples
phpstringspecial-characters

Truncated string with special character because of length


I need to limit the length of a string. I do it like this:

$str.='<div class="timt">'.substr($row['summary'],0,180).'</div>';

The problem is that in my language, there are some special chars like áéíúó , when the string "ends" in a special character, the end of the line echo h&eac instead of .
I think that's because of &eacute or something like that... but how can i solve this so this is not "truncated"?

I found some suggestions but none is working How to truncate HTML with special characters?.. is there any chance some one can point how can i solve this.

I also tried this but it's not working.

$str.='<div class="timt">'.mb_strcut($row['summary'],0,180,"UTF-8").'</div>';

After the answer of André i try this

$description = html_entity_decode($row['summary']);
$str.='<div class="timedesctt" onclick="location.href=\''.$this->make_url("item/view/".$row['itemid'],array('city_name'=>$this->get_english_city_name($row['city']))).'\';">'.mb_strcut($description,0,180,"UTF-8").'</div>';

Which now just have issue.. it's showing ? instead of á

I have my database encoded as utf8 unicode ci and the charset is set as utf8

Solution : html_entity_decode($strint,ENT_QUOTES, 'UTF-8')


Solution

  • The easiest way would be to convert your html entites with html_entity_decode(). Then use mb_substr(), and convert them back.

    I can provide with an example if necessary but check html_entity_decode().