2012-05-04 54 views
1
$fileSyntax = strtolower(preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($fileSyntax, ENT_QUOTES, 'UTF-8'))); // remove foreign character accents 
$fileSyntax = preg_replace("/[^a-zA-Z0-9\s]/", "", $fileSyntax); // remove anything that's not alphanumeric, or a space 
$fileSyntax = preg_replace("/\s+/", "-", $fileSyntax); // replace space with hyphen  
$fileSyntax = trim($fileSyntax, "-"); // removes prefixing and trailing hyphen 

替代國外字符上面的代碼將產生如下:PHP:字符串

Pokémon = pokemon 
YO MAN! = yo-man 

我想改寫這個效率,並在此後不久轉換成一個功能。

我該如何利用多個preg_replace()所以這不會是一個多行代碼?

+6

這有什麼錯4行代碼?親愛的開發人員,1巨大的行,以及1巨大的SQL查詢不是銀彈。你爲開發人員編寫代碼,以便他們可以輕鬆閱讀,所以更喜歡可讀性...其他所有內容 – zerkms

+0

理想情況下,我想將其轉換爲函數,並且我想知道當前解析字符串的方法有多糟糕。 – Aaron

+0

「我想知道我目前的方法有多糟糕」 - 是否按預期工作?如果是 - 那麼一切都很好。 – zerkms

回答

1

只是讓你知道,這條線:

$fileSyntax = preg_replace("/[^a-zA-Z0-9\s]/", "", $fileSyntax); 

應包括連字符,或者你要阻止人們能夠鍵入ice-skate,它會成爲iceskate,例如。

$fileSyntax = preg_replace("/[^a-zA-Z0-9\s-]/", "", $fileSyntax); 

空格應該真的被下劃線替換(在我看來),因爲連字符可以用在單詞中。

還你可以爲你的函數做到這一點:

function replace_chars($fileSyntax){ 
    return strtolower(
     preg_replace(
      array(
       "/&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);/i", 
       "/[^a-zA-Z0-9\s-]/i", 
       "/\s+/" 
      ), 
      array(
       "$1", // remove foreign character accents 
       "", // remove anything that's not alphanumeric, hyphen or a space 
       "_" // replace space with underscore 
      ), htmlentities($fileSyntax, ENT_QUOTES, 'UTF-8') 
     ) 
    ); 
} 

它的代碼,所有技術上一行,就隔開,使得它很容易閱讀和理解正在發生的事情。你最好把它轉到replace_chars("TeRríbLé(!) STRinG :)");應返回terrible_string

0

您可以將preg_replaces爲主題的參數,這種方式有什麼替換回報會的主題爲anothe更換等等...

0

多行代碼或函數沒有什麼不對,它更清晰地閱讀,並且與長行代碼一樣工作,這是因爲如果是系列它將保持連續和所需的時間執行將是相同的,如果你想加快這個過程,你可以嘗試使並行線程工作在同一個黑板字符串,但這將是相當複雜的(你需要解決所有的衝突問題)。

0

通過簡單地用我的超級功能:

function text2url($chaine) 
    { 
    $chaine = htmlentities($chaine, ENT_NOQUOTES, 'utf-8'); 
    $chaine = preg_replace('#\&([A-za-z])(?:uml|circ|tilde|acute|grave|cedil|ring)\;#', '\1', $chaine); 
    $chaine = preg_replace('#\&([A-za-z]{2})(?:lig)\;#', '\1', $chaine); 
    $chaine = preg_replace('#\&[^;]+\;#', '', $chaine); 
    $chaine = preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $chaine); 
    $chaine = str_replace('(', '', $chaine); 
    $chaine = str_replace(')', '', $chaine); 
    $chaine = str_replace('[', '', $chaine); 
    $chaine = str_replace(']', '', $chaine); 
    $chaine = str_replace('.', '-', $chaine); 
    $chaine = trim($chaine); 
    $chaine = str_replace(' ', '_', $chaine); 

    return $chaine; 
    } 
0

還有另一種方式來做到這一點,將你的字符串中去除僅口音。我寫了這個函數用於我的應用程序,其語言是葡萄牙語 - 意味着它具有您可以想象的所有變音符號。它的工作原理就像一個魅力:

function stripAccents($string){ 
    $accents = '/&([A-Za-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml|caron);/'; 
    $string_encoded = strtolower(htmlentities($string,ENT_NOQUOTES,'UTF-8')); 
    return $string_encoded = preg_replace($accents,'$1',$string_encoded); 

}