0

I got this error on my website this morning when I click on post-ad

I have tried looking at the code but don't seem to find anything wrong

if (!function_exists('adforest_extarct_link')) {

    function adforest_extarct_link($string) {
        $arr = explode('|', $string);
        list($url, $title, $target, $rel) = $arr; /* This is line 148 */
        $rel = urldecode(adforest_themeGetExplode($rel, ':', '1'));
        $url = urldecode(adforest_themeGetExplode($url, ':', '1'));
        $title = urldecode(adforest_themeGetExplode($title, ':', '1'));
        $target = urldecode(adforest_themeGetExplode($target, ':', '1'));
        return array("url" => $url, "title" => $title, "target" => $target, "rel" => $rel);
    }

This is the error Message

Undefined offset: 3 in /customers/7/6/1/corpersmarket.com/httpd.www/wp-content/themes/adforest/inc/theme_shortcodes/short_codes_functions.php on line 148

It actually has 3 lines of error:

Notice: Undefined offset: 1 in /customers/7/6/1/corpersmarket.com/httpd.www/wp-content/themes/adforest/inc/theme_shortcodes/short_codes_functions.php on line 148 
Notice: Undefined offset: 2 in /customers/7/6/1/corpersmarket.com/httpd.www/wp-content/themes/adforest/inc/theme_shortcodes/short_codes_functions.php on line 148 
Notice: Undefined offset: 3 in /customers/7/6/1/corpersmarket.com/httpd.www/wp-content/themes/adforest/inc/theme_shortcodes/short_codes_functions.php on line 148
Martin
  • 22,212
  • 11
  • 70
  • 132
Isaac
  • 3
  • 1
  • 3

1 Answers1

1

Question is broadly a duplicate of PHP undefined offset from list()

However,

Your list expects at least 4 prameters -- but your $arr array only has 1. So the following three are empty. (remember arrays start at 0). So your $string does not contain a | character for the explode function to work as intended.

Workaround:

Original:

    $arr = explode('|', $string);
    list($url, $title, $target, $rel) = $arr; /* This is line 148 */

Becomes:

    $arr = array_pad(explode('|', $string), 4, null);
    list($url, $title, $target, $rel) = $arr;

What this does:

Pads the array out to contain a minimum of 4 values; so that the list values will always be populated, even if they may still be empty.

Martin
  • 22,212
  • 11
  • 70
  • 132
  • That works thanks for the tutorial as I have learned something new today. – Isaac Jul 11 '19 at 11:55
  • @Isaac that's great. If this works you can click the up arrow and the tick by the answer to mark it as the best answer. Thanks! – Martin Jul 11 '19 at 12:55