I have recently upgraded from PHP5.6 to 7.3.4 , on my website I have a page containing a datatables with a lot of vacancies.
I load my DataTables using AJAX. In this function I have the following piece of code:
if (strlen($vac->name) > 19) {
$vacname = substr($vac->name, 0, 18) . "...";
} else {
$vacname = $vac->name;
}
if (!empty(LOCATION)) {
$vacNameUrl = '<a href="'.localized_base_url() . strtolower(LOCATION) . "/" . lang("url_voluntary_work_detail") . $slug . "/" . $vac->vacancy_id.'">'.$vacname.'</a>';
} else {
$vacNameUrl = '<a href="'.localized_base_url() . lang("url_voluntary_work_detail") . $slug . "/" . $vac->vacancy_id.'">'.$vacname.'</a>';
}
...
$obj->title = $vacNameUrl;
And some other code to fill in some variables in columns in the DataTables.
This worked great on php 5.6. After the upgrade we noticed that (i'm still not sure why, i cannot pinpoint it) my datatables would return an alert message with an error saying that the JSON is incorrect.
After a lot of debugging we found out that if we change the code to:
if (strlen($vac->name) > 19) {
//$vacname = substr($vac->name, 0, 18) . "...";
$vacname = $vac->name;
} else {
$vacname = $vac->name;
}
That the DataTables would work again as before. I know that the IF like I pasted it above does not make sense anymore, but I would like to have the same functionality again as before with long strings getting cut off and three dots appended at the end.
I tried reading the PHP changelogs for any change on substr, but the only thing I can find is:
substr() and iconv_substr() now return an empty string, if string is equal to start characters long.
I don't think this explains why the code stopped working.
Can anyone explain this behavior and know how to deal with this and get my substring to work again as before?