1

I have a paragraph below, that I want from it is the first word of every end of a period.

$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.";

for example, I can do this.

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

and it prints

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] => )

however, I was wandering how can one get the first word from every array. for example how is it possible to create a function that will get the first word like the example below:

Microsft 2 it's 3 9 The 4

Thanks

2
  • Try explode(' ',$paragraph) Commented Jul 4, 2012 at 1:52
  • @someGuy, be sure to include the homework tag in the future so people can properly answer the question. Commented Jul 4, 2012 at 2:34

2 Answers 2

3

Use explode() on each sentence, but use a space, instead of a period.

$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;
}
Sign up to request clarification or add additional context in comments.

7 Comments

it would skip some words, I have shared the output.
Sorry about that, forgot to trim() each sentence. Try it now.
Thanks, This the trim did the trick, but if I may ask, what the did the trim function did here.
It removes spaces from the beginning and ending of each sentence. It is important because you are exploding the sentences using spaces as separators, and if a separator (space) appears at the beginning, the first word will be an empty word and the one you are looking for would be the second.
@someGuy, well, the explode() was returning " It's worth noting..." for $sentences[2], which returned $words = Array([0] => "", [1] => "It's", ...), while you were expecting $words[0] to be "It's". The trim() call simply removed the empty array items from the beginning and end.
|
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];
  }
}

2 Comments

Thanks, but in this one, the ouput would be "MicrosoftIt'sThe", but where is the numbers.
The numbers also will come in the result ---Microsoft2It's39The4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.