Jump to content

[SOLVED] Split a paragraph into sentences?


Kane250

Recommended Posts

I'm hoping someone here has a solution for something like this.  Here is what I want to do.  I want to have a user type a paragraph of some sort into a textarea, then when submitted, I want PHP to separate that paragraph into sentences (probably by capturing all the words between periods, exclamation points, etc.), and then have it insert each sentence separately into a table... possible?  Logically it sounds like it would be, but I don't know where to begin with coding it.  Can anyone assist me in getting this started?

 

Thanks in advance!

Link to comment
Share on other sites

you can look into the implode() function which can take a block of text and use a delimiter "." or "!" and split up the text into an array based on that delimiter.

 

there may be another, simpler way to do it as well but i think this would work

 

Correct except for you should be using explode() instead.  It will return an array and you can have multiple delimiters (. ! ? etc...).

Link to comment
Share on other sites

You can't have multiple delimiters for explode.  That's what preg_split is for.  But even then, it explodes at the delimiter, so you will get sentences returned without the punctuation marks.  You can take preg_split a step farther and flag it to return them in their own elements with PREG_SPLIT_DELIM_CAPTURE and then run some loop to implode sentence and following punctation mark in the returned array, or just use preg_match_all:

 

preg_match_all('~.*?[?.!]~s',$string,$sentences);

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.