1

以下に段落がありますが、そこから欲しいのは、ピリオドの終わりごとの最初の単語です。

$paragraph="Microsoft is writing down $6.2 billion of the goodwill within its OSD. It's worth noting that at the time of the acquisition, the company recorded $5.3 billion of goodwill assigned to the OSD, bringing its total carrying balance up to $5.9 billion in September of that year. The goodwill in the OSD had climbed up to $6.4 billion by March of this year, so this accounting charge is wiping out the vast majority of that figure.";

たとえば、私はこれを行うことができます。

$sentences=explode('.',$paragraph);
print_r( $sentences);

そしてそれは印刷します

Array ( [0] => Microsoft is writing down $6
        [1] => 2 billion of the goodwill within its OSD 
        [2] => It's worth noting that at the time of the acquisition, the company recorded $5 
        [3] => 3 billion of goodwill assigned to the OSD, bringing its total carrying balance up to $5                            
        [4] => 9 billion in September of that year 
        [5] => The goodwill in the OSD had climbed up to $6 
        [6] => 4 billion by March of this year, so this accounting charge is wiping out the vast majority    of that figure 
        [7] => )

ただし、すべての配列から最初の単語を取得する方法をさまよっていました。たとえば、次の例のように最初の単語を取得する関数を作成するにはどうすればよいですか。

Microsoft 2 it's 3 9 The 4

ありがとう

4

2 に答える 2

3

explode()各文で使用しますが、ピリオドの代わりにスペースを使用してください。

$paragraph = "Microsoft is writing down $6.2 billion of the goodwill within its OSD. It's worth noting that at the time of the acquisition, the company recorded $5.3 billion of goodwill assigned to the OSD, bringing its total carrying balance up to $5.9 billion in September of that year. The goodwill in the OSD had climbed up to $6.4 billion by March of this year, so this accounting charge is wiping out the vast majority of that figure.";

$sentences = explode('.', $paragraph);

foreach($sentences as $sentence){
 $words = explode(' ', trim($sentence));
 $first = $words[0];
 echo $first;
}
于 2012-07-04T01:52:56.133 に答える
1
$paragraph="Microsoft is writing down $6.2 billion of the goodwill within its OSD. It's worth noting that at the time of the acquisition, the company recorded $5.3 billion of goodwill assigned to the OSD, bringing its total carrying balance up to $5.9 billion in September of that year. The goodwill in the OSD had climbed up to $6.4 billion by March of this year, so this accounting charge is wiping out the vast majority of that figure.";

firstWords($paragraph);

function firstWords($paragraph) {
  $sentences = explode('.', $paragraph);

  foreach ($sentences as $sentence) {
   $words = explode(' ', trim($sentence));
   echo $words[0];
  }
}
于 2012-07-04T01:56:48.797 に答える