phpstringhtml-entities

Truncate/shorten text causes encoding error on HTML Entities


I'm building a website about health. And I need to display some symptoms along with their data in a table. However, the names of some symptoms are too long, which causes a layout issue. So, I found a way to shorten their names with PHP, as you can see below:

<?=strlen($a)>45 ? stripslashes(substr($a, 0, 45)."...") : stripslashes($a)?>

It works fine. The only problem is when the string is cut in the middle of an HTML Entity, which causes the browser to display a diamond with a question mark instead (http://prntscr.com/dzqyps).

Example:

Original string:

long string with more than x characters ends j&aacute

Truncated string:

long string with more than x characters ends j&aa...

String as displayed in the browser:

long string with more than x characters ends j?...

How can I solve this issue?


Solution

  • Haha... best I can do in little time... Maybe there's a more elegant way, but it works. It's very rough and edgy still and does not account for texts that have " & " in them near the end of the first cut off string. But just to give you an idea. Hope the variable names help you figure it out. Hope it helps, good luck...

    $text = "long string with more than x characters ends j&aacute long stri";
    $maxDisplayableLength = 51; // Would cut in j&acute in half!!!
    
    $partOne = substr($text, 0, $maxDisplayableLength);
    $partTwo = substr($text, $maxDisplayableLength, (strlen($text)-$maxDisplayableLength));
    // Now go back max 8 positions (longest &-code)
    $inspectLastEightChars  = substr($partOne, -8, 8);
    $positionAmpersand      = stripos( $inspectLastEightChars, "&");
    if ($positionAmpersand !== false) { // Ohoh, '&' is found
       $correctedPartOne = substr($partOne, 0, (strlen( $partOne ) - 8 + $positionAmpersand));
       $prePendToPartTwo = substr( $inspectLastEightChars, $positionAmpersand, (strlen($inspectLastEightChars)-$positionAmpersand));
       $correctedPartTwo = $prePendToPartTwo.$partTwo;
    }
    echo('$correctedPartOne: '.$correctedPartOne.'<br />'.'$correctedPartTwo: '.$correctedPartTwo.'<br />'.'Combined: '.$correctedPartOne.$correctedPartTwo);
    

    Results:

    $correctedPartOne: long string with more than x characters ends j
    $correctedPartTwo: á long stri
    Combined: long string with more than x characters ends já long stri