Reputation: 51
I will have a $content variable, with unlimited number of text (can be any number of sentences, and paragraphs)
Every sentence in English ends, with either dot, ellypses, question mark, or exclamation mark, so these could be:
$dot = ".";
$ellypses = "...";
$question = "?";
$exclamation = "!";
What I am trying to achieve is limit $content to the first three sentences only, and get it displayed on a blog. This would mean that any combination of these (any instance) would need to be recognized, "counted to three", and than stored in another variable, which could be called $shortened_text (it does not have to be printed on a page, I will be using this with CyberSeo plugin for Wordpress).
Would you be able to suggest how to write a code for something like this (and if not possible, what php functions I should use)?
Upvotes: 0
Views: 269
Reputation: 14520
Do a subsequence search (strstr()
in PHP allows for this). Try implementing this pseudocode:
set $n equal to 3
if $content content contains $n occurrences of ("?" or "!" or "..."):
set $someFlag = True // this is just whatever flag you set to indicate that three sentences have been inputted
else if $content contains $n occurrences of "." and they are not "...":
set $someFlag = True
You could concatenate both of those branch statements into a single if
connected by an OR
, too.
EDIT: you can make this easier using substr_count()
.
Upvotes: 1