-2

How do I keep only the first 1000 occurrences of a words in a very long text and discard everything behind.

For example:

Sentence 1[ss]Sentence 2[ss]Sentence 3[ss]Sentence 4[ss]...Sentence 999[ss]Sentence 1000[ss]Sentence 1001[ss]Sentence 1002[ss]

Note: [ss] is my self-defined separator.

I would like to keep Sentence 1[ss]Sentence 2[ss]...Sentence 1000[ss]. In other words, I would like to keep EVERYTHING (including the [ss]) until the Sentence 1000[ss] and discard everything behind Sentence 1000[ss].

Barmar
  • 669,327
  • 51
  • 454
  • 560
Aeron
  • 3
  • 1
  • 1
    a) Your question is very unclear: first you talk about word occurrences, then you talk about sentences and separators. What do you want exactly? b) Have you tried anything? Why didn't it work? – Mischa Jul 05 '13 at 09:49

2 Answers2

3

You can explode the string, slice the array and then implode it again:

$sentences = explode('[ss]', $string);
$sentences = array_slice($sentences,0, 1000);
$string = implode('[ss]', $sentences);
Headshota
  • 20,343
  • 11
  • 58
  • 79
1
$array = explode('[ss]', $text);
array_splice($array, 1000);
$text = implode('[ss]', $array);
Barmar
  • 669,327
  • 51
  • 454
  • 560