2011-01-14 61 views
0
function rseo_get_seo($check, $post){ 
//code breaks somewhere in here. or in the rseo_doTheParse function. 
switch ($check) 
{ 
case "h1": return rseo_doTheParse('h1', $post); 
case "h2": return rseo_doTheParse('h2', $post); 
case "h3": return rseo_doTheParse('h3', $post); 
case "img-alt": return rseo_doTheParse('img-alt', $post); 
} 
} 

function rseo_doTheParse($heading, $post){ 
    try { //I get a FATAL error here. unexpected '{' 
     $content = $post->post_content; 

     if ($content == "") return false; 

     $keyword = trim(strtolower(rseo_getKeyword($post))); 
     @$dom = new DOMDocument; 
     @$dom->loadHTML(strtolower($post->post_content)); 
     $xPath = new DOMXPath(@$dom); 

     switch ($heading) 
     { 
      case "img-alt": return $xPath->evaluate('boolean(//img[contains(@alt, "'.$keyword.'")])'); 
      default: return $xPath->evaluate('boolean(/html/body//'.$heading.'[contains(.,"'.$keyword.'")])'); 
     } 
    } 
    catch (Exception $e) 
    { 
     echo 'Exception caught: ', $e->getMessage(), "\n"; 
    } 
} 
+3

Works [here](http://ideone.com/PjAqO) – marcog 2011-01-14 20:56:12

+0

您是不是偶然使用PHP 4? – NikiC 2011-01-14 20:57:44

+0

这很奇怪。你确定你也检查过这个函数的代码吗?它可能会因为未关闭的函数而发生,if-clause等等 - 我不确定,但是这段代码看起来是正确的。编辑:marcog的回答也强调,它与这段代码无关。所以你应该去其他地方调查。 – Paul 2011-01-14 20:57:50

回答

6

我能想到的唯一的事情就是您使用的PHP 4不支持异常处理。所以它认为try是某种常数,但不期望{在那里。

您应该得到一个解析错误,而不是致命错误。

0

我将代码粘贴到一个新文件中并运行它:没有错误。问题可能在你的代码之上?

0

第14行之后的切换块。在catch块之前删除第二个}

1

该代码是100%有效的。也许错误在别处。在附注中,DOM函数不会抛出异常 - 您可能希望查看libxml_use_internal_errors并将其设置为抛出异常。