PHPFront

Home > PHP > Print first sentence
The PHP website created by its users!



Print first sentence


Author
DescriptionThis function will print the first sentence in a given string. Great for search result pages.
RatingThis script has been rated 5/5 (11 votes)

PHP Code


<?
    
//getLeadingSentences
    //Copyright (c) 2000 Jason R. Pitoniak.  All rights reserved.
    //jason@interbrite.com http://www.interbrite.com

    //If you find this code useful, find a bug, or have a suggestion,
    //please email me.  Feel free to use this code for any purpose.

    
function getLeadingSentences($data$max)
    {
        
//given string $data, will return the first $max sentences in that string

        //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:
        
$re "^s*[^.?!]+[.?!]+s*";
        
$out "";
        for(
$i 0$i $max$i++) {
            if(
ereg($re$data$match)) {
                
//if a sentence is found, take it out of $data and add it to $out
                
$out .= $match[0];
                
$data ereg_replace($re""$data);
            }
            else {
                
$i $max;
            }
        }
        return 
$out;
    }

    
//EXAMPLE:
    
$start "Sentence one...  Sentence two?  Sentence three!  Sentence four.";
    
$end getLeadingSentences($start3);
    echo(
"result: $end");
?> 



Rate it: Print Print

Comments


No comments posted yet.

Name
Website
Your comment