2017-06-15 114 views
1

我收到此错误。 解析错误:语法错误,意外T_FUNCTION 我知道这是因为我的旧php版本,但我必须解决它在所有PHP版本上工作,因为客户端也可能有一个旧的PHP版本。 该线路上的错误:解析错误:语法错误,313行上的意外T_FUNCTION

$模式= '#(' array_reduce($ pattern_array,函数($携带,$项目){

我试着玩了,但它不工作

我的PHP代码:

function theme_oembed_videos() { 

    global $post; 

    if ($post && $post->post_content) { 

     global $shortcode_tags; 
     // Make a copy of global shortcode tags - we'll temporarily overwrite it. 
     $theme_shortcode_tags = $shortcode_tags; 

     // The shortcodes we're interested in. 
     $shortcode_tags = array(
      'video' => $theme_shortcode_tags['video'], 
      'embed' => $theme_shortcode_tags['embed'] 
     ); 
     // Get the absurd shortcode regexp. 
     $video_regex = '#' . get_shortcode_regex() . '#i'; 

     // Restore global shortcode tags. 
     $shortcode_tags = $theme_shortcode_tags; 

     $pattern_array = array($video_regex); 

     // Get the patterns from the embed object. 
     if (! function_exists('_wp_oembed_get_object')) { 
      include ABSPATH . WPINC . '/class-oembed.php'; 
     } 
     $oembed = _wp_oembed_get_object(); 
     $pattern_array = array_merge($pattern_array, array_keys($oembed->providers)); 

     // Or all the patterns together. 
     $pattern = '#(' . array_reduce($pattern_array, function ($carry, $item) { 
      if (strpos($item, '#') === 0) { 
       // Assuming '#...#i' regexps. 
       $item = substr($item, 1, -2); 
      } else { 
       // Assuming glob patterns. 
       $item = str_replace('*', '(.+)', $item); 
      } 
      return $carry ? $carry . ')|(' . $item : $item; 
     }) . ')#is'; 

     // Simplistic parse of content line by line. 
     $lines = explode("\n", $post->post_content); 
     foreach ($lines as $line) { 
      $line = trim($line); 
      if (preg_match($pattern, $line, $matches)) { 
       if (strpos($matches[0], '[') === 0) { 
        $ret = do_shortcode($matches[0]); 

       } else { 
        $ret = wp_oembed_get($matches[0]); 

       } 
       return $ret; 
      } 
     } 
    } 
} 
+0

您是否尝试过在array_reduce之外声明函数,然后像'array_reduce($ pattern_array,“arrayReduceCallback”)''传递它? PHP版本多大? <4? – Jeff

回答

0

你必须创建一个名为函数(如“myfunction”但有更好的名字)
array_reduce()接受一个字符串作为第二个参数,这是函数的名称。

function myfunction($carry, $item) { 
    if (strpos($item, '#') === 0) { 
     // Assuming '#...#i' regexps. 
     $item = substr($item, 1, -2); 
    } else { 
     // Assuming glob patterns. 
     $item = str_replace('*', '(.+)', $item); 
    } 
     return $carry ? $carry . ')|(' . $item : $item; 
}; 

... 

$pattern = '#(' . array_reduce($pattern_array, "myfunction") . ')#is'; 
+0

我把你的代码重命名为all_patterns后,但我得到这个错误:Warning:array_reduce()[function.array-reduce]:第二个参数'all_patterns',应该是一个有效的回调在C:\ AppServ \第313行 – hazemhazem

+0

www \ the-path \ functions.php是否有'array_reduce'访问'all_patterns'?就像,他们在不同的和无关的PHP文件,或在不同的范围?也许'array_reduce'认为'all_patterns'实际上并不存在... – Ryan

相关问题