2017-06-15 49 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

相關問題