使用PHP在字符串中的第n个字符之前截断直到精确符号

Let's say we have following sample string:

We have words such as fname, lname, age and year, salary, nancy, davolio, erin, borakova, tony, raphael

What I need to do is, to get the part of the string until last occurence of , delimiter and result must be up to 70th characters.

So for sample text, result must be like:

We have words such as fname, lname, age and year, salary, nancy

Is there any single function of PHP that does some operation like this?

I tried something like:

$max_allowed_len = 70;
if (strlen($str) > $max_allowed_len) {
    $cut_pos = strpos($ico_titles, ',', $max_allowed_len - 20);
    $text = substr($str, 0, $cut_pos);
}

But, I think that using

 $max_allowed_len - 20

as offset point will not give me exact results.

Here is, quick workaround for your question:

    $text="We have words such as fname, lname, age and year, salary, nancy, davolio, erin, borakova, tony, raphael";
    $max_allowed_len = 70;
    $truncated_text= substr($text, 0, $max_allowed_len);
    $result = substr($truncated_text, 0, strrpos($truncated_text, ','));
    echo $result;