de-vraag
  • 質問
  • タグ
  • ユーザー
通知:
報酬:
登録
登録すると、質問に対する返答やコメントが通知されます。
ログイン
すでにアカウントをお持ちの方は、ログインして新しい通知を確認してください。
追加された質問、回答、コメントには報酬があります。
さらに
ソース
編集
 Chris_O
Chris_O
質問

PHP Sentence case既知の単語辞書を使用して大文字の固有名詞を持つ文字列ですか?

私は単語の辞書(txtファイル)に対して単語の文字列を検索し、見つからない単語を大文字にする必要があります。

I'm trying to split the string into an array of words and check them against the unix /usr/dict/words dictionary. If a match is found for the word it gets lcfirst($word) if no match then ucfirst( $word )

辞書はfgetcsvを使って開かれ、配列に入れられます(私はfgetsを使い、行末に爆発的に試みました)。

function wnd_title_case( $string ) {
$file = fopen( "/users/chris/sites/wp-dev/trunk/core/words.txt", "rb" );
while ( !feof( $file ) ) {
    $line_of_text = fgetcsv( $file );
     $exceptions = array( $line_of_text );
}


fclose( $file );
    $delimiters = array(" ", "-", "O'");
         foreach ( $delimiters as $delimiter ) {
            $words = explode( $delimiter, $string );
            $newwords = array();
                 foreach ($words as $word) {
                if ( in_array( strtoupper( $word ), $exceptions ) ) {
          //check exceptions list for any words that should be lower case
            $word = lcfirst( $word );
            } elseif ( !in_array( $word, $exceptions ) ) {
      //everything else capitalized
            $word = ucfirst( $word );
         }
       array_push( $newwords, $word );
       }
    $string = join( $delimiter, $newwords );
   }
        $string = ucfirst( $string );
   return $string;
}

ファイルが開かれたことを確認しました。

The desired output: Sentence case title string with proper nouns capitalized.
The current output: Title string with every word capitalized

編集:

以下のJayの答えを使用して、私は実行可能なソリューションを考え出しました。私の最初の問題は、私の単語辞書には、大文字と小文字の両方の単語が含まれていたので、正規表現コールバックの使用をチェックするための適切な名前辞書が見つかりました。それは完璧ではありませんが、ほとんどの場合それを正しく取得します。

function title_case( $string ) {
    $fp = @fopen( THEME_DIR. "/_/inc/propernames", "r" );  
        $exceptions = array();
        if ( $fp ) {

            while( !feof($fp) ) {
                    $buffer = fgets( $fp );
                array_push( $exceptions, trim($buffer) );
            }

        }

    fclose( $fp );

    $content = strtolower( $string );
    $pattern = '~\b' . implode ( '|', $exceptions ) . '\b~i';
    $content =  preg_replace_callback (  $pattern, 'regex_callback', $content  );
    $new_content =  $content;

    return ucfirst( $new_content );
}

    function regex_callback ( $data ) {
        if ( strlen( $data[0] )  > 3 )
        return ucfirst( strtolower( $data[0] ));
        else return ( $data[0] );

    }

2 2011-10-27T17:58:42+00:00 1
 Chris_O
Chris_O
編集された質問 11日 1月 2012 в 1:51
プログラミング
fopen
regex
string
php
fgets
Jay Gilford
26日 11月 2011 в 5:21
2011-11-26T17:21:47+00:00
さらに
ソース
編集
#56791225

正規表現でこれを行う最も簡単な方法は、以下を行うことです

  1. convert your text to all uppercase first letters $content = ucwords($original_content);
  2. Using your array of words in the dictionary, create a regex by imploding all your words with a pipe character |, and surrounding it with boundary markers and delimiters followed by the case insensitive flag, so you would end up with ~\bword1|word2|word3\b~i (obviously with your large list)
  3. create a function to lower the matched value using strtolower to be used with preg_replace_callback

作業デモの例は次のとおりです。

function regex_callback($data) {
    return strtolower($data[0]);
}

$original_content = 'hello my name is jay gilford';
$words = array('hello', 'my', 'name', 'is');

$content = ucwords($original_content);
$pattern = '~\b' . implode('|', $words) . '\b~i';

$content = preg_replace_callback($pattern, 'regex_callback', $content);

echo $content;

You could also optionally use strtolower to begin with on the content for consistency. The above code outputs hello my name is Jay Gilford

1
0
関連コミュニティ 1
PHP - 日本のコミュニティ
PHP - 日本のコミュニティ
5 ユーザー
このグループではPHPについて話します。 @vue_ja @react_ja @js_ja @angular_ja
開く telegram
質問の追加
カテゴリ
すべて
技術情報
文化・レクリエーション
生活・芸術
科学
プロフェッショナル
事業内容
ユーザー
すべて
新しい
人気
1
Roxana Elizabeth CASTILLO Avalos
登録済み 1週間前
2
Hideo Nakagawa
登録済み 1週間前
3
Sergiy Tytarenko
登録済み 1週間前
4
shoxrux azadov
登録済み 1週間前
5
Koreets Koreytsev
登録済み 2週間前
© de-vraag :年
ソース
stackoverflow.com
ライセンス cc by-sa 3.0 帰属