Kane250 Posted March 20, 2009 Share Posted March 20, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/150388-solved-split-a-paragraph-into-sentences/ Share on other sites More sharing options...
lonewolf217 Posted March 20, 2009 Share Posted March 20, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/150388-solved-split-a-paragraph-into-sentences/#findComment-789799 Share on other sites More sharing options...
Maq Posted March 20, 2009 Share Posted March 20, 2009 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...). Quote Link to comment https://forums.phpfreaks.com/topic/150388-solved-split-a-paragraph-into-sentences/#findComment-789820 Share on other sites More sharing options...
.josh Posted March 20, 2009 Share Posted March 20, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/150388-solved-split-a-paragraph-into-sentences/#findComment-789827 Share on other sites More sharing options...
Maq Posted March 20, 2009 Share Posted March 20, 2009 You can't have multiple delimiters for explode. That's what preg_split is for. Yep sorry about that Regex definitely is better here for reasons CV mentioned Quote Link to comment https://forums.phpfreaks.com/topic/150388-solved-split-a-paragraph-into-sentences/#findComment-789835 Share on other sites More sharing options...
Kane250 Posted March 21, 2009 Author Share Posted March 21, 2009 this sounds like exactly what I need. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/150388-solved-split-a-paragraph-into-sentences/#findComment-790360 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.