我在下面有一个段落,我希望它是句号每个结尾的第一个单词。
$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] => )
然而,我在徘徊,怎么能从每个阵列得到第一个字。
例如,如何创建一个能够获得第一个单词的函数,如下例所示:
的Microsft 2 它的 3 9 该 4
谢谢
答案 0 :(得分: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;
}
答案 1 :(得分: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];
}
}