2013-02-24 84 views
0

我有模型WikiPage。 WikiPage->我從數據庫中獲得文本。我做了方法「wikify」,並通過class :: wikify($ Page-> getText())在頁面上生成文本。 在文本我有建設[鏈接名],我生成的鏈接與此:維基風格的鏈接。如果目標頁面不存在,如何更改鏈接的顏色

$text = preg_replace('@\[(.*) (.*)\]@', '<a href="\\1" class="<?php Wiki::httpresponse($\\1) ?>">\\2</a>', $text); 

的想法是檢查與功能的HttpResponse和變化類的網址,如果沒有頁面。

httpresponce:

static public function httpresponse($url){ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_NOBODY, true); 
    curl_setopt($ch, CURLOPT_FAILONERROR, true); 
    curl_exec ($ch); 
    $intReturnCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    curl_close ($ch); 
    if ($intReturnCode != 200 && $intReturnCode != 302 && $intReturnCode != 304) { return "red"; } else return "blue"; 
} 

2個問題:

  1. 當我測試httpresponce在我的本地網頁它不容易找到他們,當我測試它在網頁上,一切正常。

    (如http://localhost:8080/category/page檢查鏈接,類別:第一模塊,頁面isecond,主頁是模塊:類別,動作:指數類/頁是模塊路徑:頁面,動作:秀)

  2. 類生成的鏈接仍然爲<?php Wiki::httpresponse($\\1) ?>方法httpresponce未執行。

可以做些什麼? 也許有更好的方法來完成這項任務?

回答

0

找到解決辦法。

$reg_inURL = "@\[(\w*)\/(\w*) (.*)\]\]@"; 
    preg_match_all($reg_inURL, $text, $matches); 
    $count = 0; 
    $links = count($matches[0]); 
    foreach ($matches[0] as $pattern) 
    { 
    $cat = $matches[1][$count]; 
    $pag = $matches[2][$count]; 

    $q = WikiPageQuery::create() 
     ->leftJoin('WikiPage.WikiCategory') 
     ->where('WikiCategory.category_address = ?', $cat) 
     ->filterByAddress($pag) 
     ->findOne(); 
    if (!$q) 
    { 
     $link_class = 'red'; 
     $url = $cat.'/add'; 
    } 
    else 
    { 
     $link_class = 'blue'; 
     $url = $cat.'/'.$pag; 
    } 

    $title = $matches[3][$count]; 
    $text = str_replace ($pattern, '<a href='.$url.' class='.$link_class.'>'.$title.'</a>', $text); 
    $count = $count+1; 
    } 
+0

不要忘記接受你自己的答案 – j0k 2013-02-25 08:26:32