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
3
  • looks like you are not passing proper parameter to adforest_extarct_link function, $string = "url|title|target|rel" Commented Jul 11, 2019 at 11:13
  • ok so I should add the rest code after $string? @sureshbambhaniya Commented Jul 11, 2019 at 11:21
  • Possible duplicate of PHP undefined offset from list() Commented Jul 11, 2019 at 11:41

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That works thanks for the tutorial as I have learned something new today.
@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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.