Javascript | PHP | |
---|---|---|
Initialization | var three_sentences="The quick brown fox jumps over the lazy dog. The rain in Spain falls mainly on the plain. Many hands make light work."; | $three_sentences="The quick brown fox jumps over the lazy dog. The rain in Spain falls mainly on the plain. Many hands make light work."; |
Extracting first sentence | var first_sentence=three_sentences.split('.')[0] + '.'; | $first_sentence=explode('.', $three_sentences)[0] . '.'; |
Extracting last sentence (assuming there are at least 2 sentences, for first idea of the two) | var last_sentence=three_sentences.split('.')[eval(-2 + three_sentences.split('.').length)].trim() + '.'; // or you might prefer (our more usual eval(-1 + ?.length) arrangement) var last_sentence=three_sentences.split('. ')[eval(-1 + three_sentences.split('. ').length)].trim(); | $last_sentence=ltrim(explode('.', $three_sentences)[(-2 + sizeof(explode('.', $three_sentences)))]) . '.'; // or you might prefer (our more usual (-1 + sizeof(?)) arrangement) $last_sentence=ltrim(explode('. ', $three_sentences)[(-1 + sizeof(explode('. ', $three_sentences)))]); |
Picking on "Spain" (assuming you know "Spain" exists just the once) | var no_Spain=three_sentences.split(' Spain ')[0] + ' ' + three_sentences.split(' Spain ')[1]; | $no_Spain=explode(' Spain ', $three_sentences)[0] . ' ' . explode(' Spain ', $three_sentences)[1]; |
Extracting count of second sentence words (assuming there are at least 2 sentences) | var count_second_sentence_words=eval(-1 + three_sentences.split('. ')[1].split(' ').length); | $count_second_sentence_words=sizeof(-1 + explode(' ', explode('. ', $three_sentences)[1]); |
How many "o"'s in first sentence | var count_o_first_sentence=eval(-1 + three_sentences.split('.')[0].split('o').length); | $count_o_first_sentence=sizeof(-1 + explode('o', explode('.', $three_sentences)[0])); |