- substr: documentation ( source)
- var_dump: documentation ( source)
- mb_substr: documentation ( source)
- mb_strlen: documentation ( source)
- strrpos: documentation ( source)
<?php
$testString = "dies ist eine testfirma mit mehr als fünfzig zeichen";
var_dump(breakUpWords($testString, 50));
var_dump(beforeLast($testString, 50));
function beforeLast($str, $before)
{
$posUntil = strrpos($str, $before);
if ($posUntil === false) {
return $str;
}
return substr($str, 0, $posUntil);
}
function breakUpWords(String $words, int $index) {
if (mb_strlen($words) > $index) {
return mb_substr($words, 0, 50);
}
return $words;
}