From the course: PHP Tips, Tricks, and Techniques

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Extract complete sentences from start of text

Extract complete sentences from start of text

From the course: PHP Tips, Tricks, and Techniques

Extract complete sentences from start of text

- [David] Hi, I'm David Powers and welcome to this week's edition of php Tips, Tricks and Techniques, designed to help you become a smarter, more productive php developer. This week I want to look at how to split a string using a regular expression. This can be useful, for example, to extract complete sentences from the beginning of a long piece of text. You can then display the first few sentences and add a link to the rest. The exercise files for this video contain this extract from Alice in Wonderland. And the problem soon becomes obvious. php has no concept of what constitutes a sentence, and you can't simply rely on the sentences ending with a period. For example, this sentence ends with an exclamation mark, here we've got a question mark followed by a closing quote, and then down here we've got a period followed by a closing parenthesis. And that's why we need a regular expression. So, let's switch to this other file where I've started building the script. It begins by getting…

Contents