Does anyone have a piece of PHP code to capture the first "sentence" in a string?

If I have a description like:

“We prefer questions that you can answer rather than just discuss. Provide details. Write clearly and simply.”

And all I want is:

"We prefer questions that can be answered rather than just discussed."

I suppose I would look for a regular expression such as "[.! \?]", Define strpos, and then make a substring from the main line, but I think this is a common thing, so I hope that anyone has a piece of code around.

+9
string php code-snippets
source share
8 answers

A slightly more expensive expression, however, will be more adaptable if you want to select several types of punctuation marks as sentence delimiters.

$sentence = preg_replace('/([^?!.]*.).*/', '\\1', $string); 

Find completion characters after space

 $sentence = preg_replace('/(.*?[?!.](?=\s|$)).*/', '\\1', $string); 
+20
source share
 <?php $text = "We prefer questions that can be answered, not just discussed. Provide details. Write clearly and simply."; $array = explode('.',$text); $text = $array[0]; ?> 
+8
source share

The previous regular expression seemed to work in the test, but not in real PHP. I edited this answer to provide complete working PHP code and improved regular expression.

 $string = 'A simple test!'; var_dump(get_first_sentence($string)); $string = 'A simple test without a character to end the sentence'; var_dump(get_first_sentence($string)); $string = '... But what about me?'; var_dump(get_first_sentence($string)); $string = 'We at StackOverflow.com prefer prices below US$ 7.50. Really, we do.'; var_dump(get_first_sentence($string)); $string = 'This will probably break after this pause .... or won\'t it?'; var_dump(get_first_sentence($string)); function get_first_sentence($string) { $array = preg_split('/(^.*\w+.*[\.\?!][\s])/', $string, -1, PREG_SPLIT_DELIM_CAPTURE); // You might want to count() but I chose not to, just add return trim($array[0] . $array[1]); } 
+3
source share

Try this:

 $content = "My name is Younas. I live on the pakistan. My email is **fromyounas@gmail.com** and skype name is "**fromyounas**". I loved to work in **IOS development** and website development . "; $dot = "."; //find first dot position $position = stripos ($content, $dot); //if there a dot in our soruce text do if($position) { //prepare offset $offset = $position + 1; //find second dot using offset $position2 = stripos ($content, $dot, $offset); $result = substr($content, 0, $position2); //add a dot echo $result . '.'; } 

Exit:

My name is Yunas. I live in Pakistan.

+3
source share

Try this:

 reset(explode('.', $s, 2)); 
0
source share
 current(explode(".",$input)); 
0
source share

I would probably use any of the many substrings / string functions in PHP (some of which are already mentioned). But also search for "." OR ". \ N" (and possibly ". \ N \ r") instead of "." . Just in case, for some reason, the sentence contains a period followed by a space. I think this will harden the likelihood of real results.

Example, search only "." on:

 "I like stackoverflow.com." 

You'll get:

 "I like stackoverflow." 

When really, I'm sure you will prefer:

 "I like stackoverflow.com." 

And as soon as you have this basic search, you are likely to come across one or two cases where it might miss something. Tune in as you launch it!

0
source share

This is a really serious problem. I recommend exploring the NLP package if you need reliable results. The tokenizer can identify the end characters of the sentence (or "?", ".", ";" Etc. Depending on your intended use), and you can split them up.

-one
source share

All Articles