2012-05-21 48 views
2

我對正則表達式不太擅長,但我想用它從字符串中提取單詞。使用preg_match_all從字符串中提取單詞

我需要的單詞應該至少有4個字符,並且提供的字符串可以是utf8。

例字符串:

Sus的azahares presentan gruesospétalos科斯teñidos德羅莎övioláceo烯LA單方面外耳炎,CON numerosos estambres(20-40)。

所需的輸出:

Array(
    [0] => azahares 
    [1] => presentan 
    [2] => gruesos 
    [3] => pétalos 
    [4] => blancos 
    [5] => teñidos 
    [6] => rosa 
    [7] => violáceo 
    [8] => parte 
    [9] => externa 
    [10] => numerosos 
    [11] => estambres 
) 
+0

使用爆炸功能 –

+0

爆炸不好,我也知道如何使用str_word_count,並使其發揮作用與一個特殊的charlist,但然後我必須使用一個foreach從每個單詞中統計字符,我發現這是一個不必要的步驟。 – Gabriel

+0

如果這讓你感覺更好,你可以使用array_filter而不是foreach:http://codepad.viper-7.com/AR0AEn – Gordon

回答

6

如果要查找的單詞是UTF-8(至少4個字符長,根據規格),由ISO-8859-15的字母字符組成(這適用於西班牙語,但也適用於英語,德語,法語等):

$n_words = preg_match_all('/([a-zA-Z]|\xC3[\x80-\x96\x98-\xB6\xB8-\xBF]|\xC5[\x92\x93\xA0\xA1\xB8\xBD\xBE]){4,}/', $str, $match_arr); 
$word_arr = $match_arr[0]; 
+0

這工作。我沒有注意到它的任何問題,並希望我不會找到任何問題。 Thx @Walter Tross – Gabriel

+0

對我非常有幫助:) –

1

爆炸帶空格的字符串(這將創造所有單詞的數組),然後檢查這個詞比4個字母更大。

//The string you want to explode 
$string = "Sus azahares presentan gruesos pétalos blancos teñidos de rosa o violáceo en la parte externa, con numerosos estambres." 
//explode your $string, which will create an array which we will call $words 
$words = explode(' ', $string); 

//for each $word in $words 
foreach($words as $word) 
{ 
    //check if $word length if larger then 4 
    if(strlen($word) > 4) 
    { 
     //echo the $word 
     echo $word; 
    } 
} 

strlen();

的strlen - 獲取字符串長度

explode();

爆炸 - 由字符串分割字符串

+1

按空間分解不好。此外,我試圖避免額外的foreach來計算每個單詞的字符數。 – Gabriel

+0

如果這是逗號和點,你擔心只是做一個str_replace來擺脫它們。而且你不需要額外的foreach循環來計算單詞的長度。只要說echo strlen($ word);在目前的foreach – Bono

0
$string = Sus azahares presentan gruesos pétalos blancos teñidos de rosa o violáceo en la parte externa, con numerosos estambres 

$words = explode(' ', $string); 
echo $words[0]; 
echo $words[1]; 

+0

這個解決方案不好。有些詞不是按照空格,有時它們有逗號或?要麼 !。 – Gabriel

+0

將你的字符串存儲在一個數組中,如果它有一個逗號旅行,然後保存它的長度是4或更多的單詞,每個單詞一個接一個。我在課上其他我會和我一起嘗試:) –

6

您可以使用下面的正則表達式簡單的字符串。它會匹配任何非空白字符最小長度= 4

preg_match_all('/(\S{4,})/i', $str, $m); 

現在$m[1]包含要在陣列。

更新:

正如戈登說,該模式也將匹配 '(20-40)'。不需要的數字可以使用此正則表達式中刪除:

preg_match_all('/(\pL{4,})/iu', $str, $m); 

但我認爲,如果PCRE與UTF-8支持編譯它纔會起作用。見PHP PCRE (regex) doesn't support UTF-8?。它雖然在我的電腦上工作。

+0

它也將匹配「(20-40)」,雖然 – Gordon

+0

我知道我可以用正則表達式來完成這項工作,只是我不知道如何編寫模式。你是一個好的開始,但我希望有人改善模式。 – Gabriel

+0

@bdsnoobz這種模式是可以的,除了一個單詞,所有其他都沒關係。我無法使用u修飾符,但沒有它,它運行得非常好。 – Gabriel

0

試試這個:

$str='Sus azahares presentan gruesos pétalos blancos teñidos de rosa o violáceo en la parte externa, con numerosos estambres (20-40).'; 
preg_match_all('/([^0-9\s]){4,}/i', $str, $matches); 
echo '<pre>'; 
var_dump($matches); 
echo '</pre>'; 
+0

oops,在字符類[^]內部缺少逗號符號'/([^ 0-9 \ s,]){4,}/i'符號用於否定結果。只是測試和輸出應該像預期的結果 –

+0

對不起,這並沒有得到我期望的結果。 – Gabriel

1

當您使用u修改,你可以使用下面的模式(demo):

preg_match_all('(\w{4,})u', $string, $matches); 
print_r($matches[0]); 

u modifier表示:

ü (PCRE_UTF8):該修飾符打開與Perl不兼容的PCRE的其他功能。模式字符串被視爲UTF-8。這個修飾符可以從Unix上的PHP 4.1.0或更高版本和win32上的PHP 4.2.3中獲得。從PHP 4.3.5開始,檢查該模式的UTF-8有效性。

+0

這個解決方案我無法得到任何好結果,但是我確實無法使用u修飾符。 Thx無論如何 – Gabriel

+0

對我來說,它工作得很好。 – acme

2

應該做的工作適合你

function extractCommonWords($string) { 
    $stopWords = array('i','a','about','an','and','are','as','at','be','by','com','de','en','for','from','how','in','is','it','la','of','on','or','that','the','this','to','was','what','when','where','who','will','with','und','the','www'); 

    $string = preg_replace('/\s\s+/i', '', $string); //echo $string, "<br /><br />"; // replace whitespace 
    $string = trim($string); // trim the string 
    $string = preg_replace('/[^a-zA-Z0-9 -_]/', '', $string); // only take alphanumerical characters, but keep the spaces and dashes too… 
    $string = strtolower($string); // make it lowercase 

    preg_match_all('/([a-zA-Z]|\xC3[\x80-\x96\x98-\xB6\xB8-\xBF]|\xC5[\x92\x93\xA0\xA1\xB8\xBD\xBE]){4,}/', $string, $matchWords); 
    $matchWords = $matchWords[0]; 

    foreach($matchWords as $key => $item) { 
     if($item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3) { 
      unset($matchWords[$key]); 
     } 
    } 

    $wordCountArr = array(); 
    if(is_array($matchWords)) { 
     foreach($matchWords as $key => $val) { 
      $val = strtolower($val); 
      if(isset($wordCountArr[$val])) { 
       $wordCountArr[$val]++; 
      } else { 
       $wordCountArr[$val] = 1; 
      } 
     } 
    } 

    arsort($wordCountArr); 
    $wordCountArr = array_slice($wordCountArr, 0, 10); 
    return $wordCountArr; 
} 
相關問題