0

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?

Dennis
  • 3,044
  • 2
  • 33
  • 52
  • Can you give an example of a long string, please? – Dharman May 08 '19 at 17:08
  • There is no reason for the code to generate an error. The only thing I cannot be certain about is the `$vac->name`, but given it was given to `strlen()` on the previous line, I don't think that could be the problem either. You have to look at the JSON it is complaining about, see what's there. Use your browser developer tools. – KIKO Software May 08 '19 at 17:08
  • 2
    Maybe a problem of encoding, if JSON is not valid ? `mb_substr()` is safer. – Joffrey Schmitz May 08 '19 at 17:09
  • @JoffreySchmitz That is not very likely, given that it worked before. – KIKO Software May 08 '19 at 17:10
  • I can't reproduce: https://3v4l.org/Jn078 – Dharman May 08 '19 at 17:11
  • How are you creating the actual json? – aynber May 08 '19 at 17:17
  • I am pushing all the PHP objects in a PHP array and after all the (3000+ vacancies) are pushed in the array, I do echo json_encode(array('data' => $data)); – Dennis May 08 '19 at 17:19
  • @Dharman there is nothing special about the long string, it might look like this "Help me do groceries on tuesday evening" – Dennis May 08 '19 at 17:20
  • 1
    Try to switch to `mb_substr` and check if it makes a difference. I see no problem with your code. There might be a problem with the data or somewhere else in the code. – Dharman May 08 '19 at 17:22
  • @JoffreySchmitz That actually did the trick!! mb_substr works for me while substr returned the error. Why did this not throw an error in php5.6 ? Thanks a lot! – Dennis May 08 '19 at 17:25
  • We still don't know what error you were getting in PHP 7.3 Was it JSON error or JS error or PHP exception? – Dharman May 08 '19 at 17:30
  • @Dharman I was not getting any error in my PHP or JS. It was DataTables plugin that showed an alert box saying the JSON was incorrect. So my best guess is that substr() did some wrong encoding somewhere and screwed up my JSON, so datatables cannot read it properly – Dennis May 08 '19 at 17:35
  • Check in the browser console what JSON you were getting. I know you might think you have solved the problem, but if you do not know the root cause you might face this problem again and not know how to fix it. – Dharman May 08 '19 at 17:36
  • @Dharman Thanks I will do that and report back if I find the root cause! – Dennis May 08 '19 at 17:41
  • To make it easier, so you don't have to inspect the JSON yourself, you could use [https://jsonlint.com/](https://jsonlint.com) or any other online JSON validator. – KIKO Software May 08 '19 at 17:45
  • @Dennis try to use $vacname = substr($vac->name, "0", "18") . "..."; instead of $vacname = substr($vac->name, 0, 18) . "..."; – stan chacon May 08 '19 at 22:19

0 Answers0