non sarà il codice più pulito ed efficiente, ma dovrebbe funzionare...

Codice PHP:
if (strlen($mex) > 100) {
    
$i 100;
    while(
substr($mex$i1) != " ")
        
$i--;
    
$mex substr($mex0$i);