Here’s a cool function i found today while i was working on a project.
it prints the first n sentence.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
<span style="color: #000000;"><span style="color: #007700;">function </span><span style="color: #0000bb;">getLeadingSentences</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$data</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$max</span><span style="color: #007700;">)
{
</span><span style="color: #ff8000;">//given string $data, will return the first $max sentences in that string</span></span>
//in: $data = the string to parse, $max = maximum # of sentences to return
//returns: string containing the first $max sentences
//(If the # of sentences in the string is less than $max,
//then entire string will be returned.)
//a sentence is any charactors except ., !, and ?
//any number of times, plus one or more .s, ?s, or !s
//and any leading or trailing whitespace:
<span style="color: #0000bb;">$re </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"^s*[^.?!]+[.?!]+s*"</span><span style="color: #007700;">;
</span><span style="color: #0000bb;">$out </span><span style="color: #007700;">= </span><span style="color: #dd0000;">""</span><span style="color: #007700;">;
for(</span><span style="color: #0000bb;">$i </span><span style="color: #007700;">= </span><span style="color: #0000bb;">0</span><span style="color: #007700;">; </span><span style="color: #0000bb;">$i </span><span style="color: #007700;">< </span><span style="color: #0000bb;">$max</span><span style="color: #007700;">; </span><span style="color: #0000bb;">$i</span><span style="color: #007700;">++) {
if(</span><span style="color: #0000bb;">ereg</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$re</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$data</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$match</span><span style="color: #007700;">)) {
</span><span style="color: #ff8000;">//if a sentence is found, take it out of $data and add it to $out
</span><span style="color: #0000bb;">$out </span><span style="color: #007700;">.= </span><span style="color: #0000bb;">$match</span><span style="color: #007700;">[</span><span style="color: #0000bb;">0</span><span style="color: #007700;">];
</span><span style="color: #0000bb;">$data </span><span style="color: #007700;">= </span><span style="color: #0000bb;">ereg_replace</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$re</span><span style="color: #007700;">, </span><span style="color: #dd0000;">""</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$data</span><span style="color: #007700;">);
}
else {
</span><span style="color: #0000bb;">$i </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$max</span><span style="color: #007700;">;
}
}
return </span><span style="color: #0000bb;">$out</span><span style="color: #007700;">;
}</span>
<span style="color: #ff8000;">//EXAMPLE:
</span><span style="color: #0000bb;">$start </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"Sentence one... Sentence two? Sentence three! Sentence four."</span><span style="color: #007700;">;
</span><span style="color: #0000bb;">$end </span><span style="color: #007700;">= </span><span style="color: #0000bb;">getLeadingSentences</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$start</span><span style="color: #007700;">, </span><span style="color: #0000bb;">3</span><span style="color: #007700;">);
echo(</span><span style="color: #dd0000;">"result: $end"</span><span style="color: #007700;">); </span>
[source]

2 Comments
Heh…I wrote that code for a project I did eons ago. Glad you found it useful.
Amazing how old things get dredged up. Thanks!
I just added this line before the return
if (empty($out) && !empty($data)) $out = $data;
One Trackback/Pingback
[...] print first N sentences | Just a bunch of code [...]