2

正規表現は苦手ですが、文字列から単語を抽出するために使用したいと思います。

必要な単語は4文字以上で、提供される文字列はutf8にすることができます。

文字列の例:

Sus azaharespresentangruesospétalosblancosteñidosderosaovioláceoenlaparteexterna、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
)
4

7 に答える 7

8

これは、検索する単語が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];
于 2012-05-21T12:44:35.330 に答える
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)がUTF-8をサポートしていないを参照してください。。それは私のコンピューターでも動作します。

于 2012-05-21T11:28:03.073 に答える
3

それはあなたのために仕事をするはずです

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;
}
于 2014-02-18T04:52:19.220 に答える
2

修飾子を使用するu場合、次のパターンを使用できます(デモ)。

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

u修飾子の意味:

u(PCRE_UTF8):この修飾子は、Perlと互換性のないPCREの追加機能をオンにします。パターン文字列はUTF-8として扱われます。この修飾子は、UnixではPHP 4.1.0以降、win32ではPHP4.2.3から使用できます。PHP 4.3.5以降、パターンのUTF-8の有効性がチェックされます。

于 2012-05-21T11:50:37.440 に答える
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();

explode —文字列を文字列ごとに分割します

于 2012-05-21T11:21:50.003 に答える
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];

等々

于 2012-05-21T11:22:57.163 に答える
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>';
于 2012-05-21T11:47:49.767 に答える