Hello,
This may sound more complicated than it is, but I'll try my best to explain the question.
Suppose I have five input strings:
String one:
This is a string of four sentences. The second sentence is this one. All four sentences have periods at the end. Should be simple.
String two:
This is a string of four lines.
The second line / sentence is this one, but it doesn't end with a period
All four lines have periods and newline at the end.
This is getting complicated.
String three:
This is a string of four sentences. The second sentence ends with two exclamation marks!! How about the third sentence? Oh my!
String four:
Is this a string of four sentences or four lines?
I'm not sure anymore. Second line contains two sentences, which makes things complicated.
I think I'm going crazy now
String five:
What if the string is only one sentence?
From all the five sample strings, what is the easiest way of getting only the first two sentences from each strings?
Our current solution is in the attached code. As you can see from the output, it works to an extent. Third string is wrong because it shows three sentences (due to second one doesn't end with period)
One of the problems we have is if the sentence are separated line by line, but they don't end with period, exclamation, or question mark. Then our regex will fail. We can't use ^\w because that will also grab punctuations like comma, colon, etc.
The other problem is that we have a feeling there's easier way to do this than what we currently have.
As a side information, the four strings will be reviews submitted by user to a site. We want to make it such that we only show up to two sentences from whatever the user enters.
So, are we on the right track? Or is there an easier way to accomplish this in PHP? Any suggestions for improvements?
I'd like to say 'scrap it and just do review cutoff by strlen!!', but hey ... i'm just a grunt -(
thank you.
-stndn.
Sample output:
This may sound more complicated than it is, but I'll try my best to explain the question.
Suppose I have five input strings:
String one:
This is a string of four sentences. The second sentence is this one. All four sentences have periods at the end. Should be simple.
String two:
This is a string of four lines.
The second line / sentence is this one, but it doesn't end with a period
All four lines have periods and newline at the end.
This is getting complicated.
String three:
This is a string of four sentences. The second sentence ends with two exclamation marks!! How about the third sentence? Oh my!
String four:
Is this a string of four sentences or four lines?
I'm not sure anymore. Second line contains two sentences, which makes things complicated.
I think I'm going crazy now
String five:
What if the string is only one sentence?
From all the five sample strings, what is the easiest way of getting only the first two sentences from each strings?
Our current solution is in the attached code. As you can see from the output, it works to an extent. Third string is wrong because it shows three sentences (due to second one doesn't end with period)
One of the problems we have is if the sentence are separated line by line, but they don't end with period, exclamation, or question mark. Then our regex will fail. We can't use ^\w because that will also grab punctuations like comma, colon, etc.
The other problem is that we have a feeling there's easier way to do this than what we currently have.
As a side information, the four strings will be reviews submitted by user to a site. We want to make it such that we only show up to two sentences from whatever the user enters.
So, are we on the right track? Or is there an easier way to accomplish this in PHP? Any suggestions for improvements?
I'd like to say 'scrap it and just do review cutoff by strlen!!', but hey ... i'm just a grunt -(
thank you.
-stndn.
Sample output:
before:
String 0: This is a string of four sentences. The second sentence is this one. All four sentences have periods at the end. Should be simple.
String 1: This is a string of four lines.
The second line / sentence is this one, but it doesn't end with a period
All four lines have periods and newline at the end.
This is getting complicated.
String 2: This is a string of four sentences. The second sentence ends with two exclamation marks!! How about the third sentence? Oh my!
String 3: Is this a string of four sentences or four lines?
I'm not sure anymore. Second line contains two sentences, which makes things complicated.
I think I'm going crazy now
String 4: What if the string is only one sentence?
after:
String 0: This is a string of four sentences. The second sentence is this one. -----
String 1: This is a string of four lines. The second line / sentence is this one, but it doesn't end with a period All four lines have periods and newline at the end. -----
String 2: This is a string of four sentences. The second sentence ends with two exclamation marks!! -----
String 3: Is this a string of four sentences or four lines? I'm not sure anymore. -----
String 4: What if the string is only one sentence? -----