2011-07-06 51 views
0

我有一個名爲「load.php」的頁面,它在每個頁面的頂部被調用。它有一些各種preg_replace()函數和strtolower()函數,它們影響頁面末尾的$ text1變量。 (這個改變是在加載頁面時完成的,而不是插入到db中) 我想在strtolower()之前或之後添加一個最終函數,以從strtolower()中排除URL的href屬性。我如何管理這個?謝謝。除URL之外的小寫字母

+0

你可以提供一個代碼片段?目前還不清楚你想要做什麼。 – Emyr

+0

是的,我們希望看到來源(只有幾條重要的線)來理解這個問題。我認爲你可以用正則表達式檢查你的文本是否是URL,然後使用你的strtolower函數。 – Andron

回答

0

讓我嘗試:

//search for links with href 
$links = preg_match_all('/href="(?P<link>[^"]*?)"/i',$text1, $matches); 
if(count($matches['link'])>0){ 
    // explode non links pieces of code 
    $blocks = preg_split('/href="(?P<link>[^"]*?)"/i',$text1); 
    // for assurance 
    // non-links pieces should be equal a links plus one 
    if(count($matches['link']) == (count($blocks)-1)) 
    { 
     // to lower non-link pieces 
     $blocks = array_map("strtolower", $blocks); 
     $size = count($matches['link']); 
     for($i=0;$i<$size;$i++){ 
      //putting together the link again without change a case 
      $blocks[$i] .= 'href="'.$matches['link'][$i].'"'; 
     } 
     $text1 = join("",$blocks); 
    } 
} else { 
    $text1 = strtolower($text1); 
} 

搖頭好運:)

0

在這裏,你已經一個較短的版本:

function strtolowerExceptLinks($text) { 
     $search = '(\b[a-zA-Z0-9]+://[^(|\>\n)]+\b)'; 
     preg_match_all($search, $text, $matches); 
     $urls = array_unique($matches[0]); 
     $text = mb_strtolower($text); 
     if (is_array($urls)) { 
      foreach ($urls as $url) { 
       $text = str_replace(mb_strtolower($url), $url, $text); 
      } 
     } 
     return $text; 
    } 
相關問題