Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filtering results

Status
Not open for further replies.

DeepBlerg

Technical User
Jan 13, 2001
224
AU
Hi,

I have a page that queries a MySQL database and brings back the results. What I want to do is on one of the results, limit it to the first sentence.

eg: so if $description = "Hi there. It's a nice day."

I'd like it to just return "Hi there."

How do I do this?

Thanks.
 
Well there are several ways, You can count letters or words but i think you need to explode the string at the dot.

parts = explode(".", $sequence);
firstSentence = parts[0]; mcvdmvs
-- "It never hurts to help" -- Eek the Cat
 
that's a very tricky thing to do. It works in 99% of the cases, but if you have a dot in the middle of the sentence that is not to end the sentence, the program will fail.

For example,

"Hello Dr. Doolitle. How are you?"
or
"How are you Dr. Doolitle? Everything fine?"

In both cases fails.

You should parse the sentence to see what means the dot in that case, or if the sentence is is terminated by another char.


But, realy is the best solution. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Hey mcvdmvs,

Thanks for your help. Problem is I won't know exactly how many parts there will be each time. How do I turn your solution into a "while" loop?
 
while ($row = mysql_fetch_array($result, MYSQl_ASSOC)) {
$parts = explode($row['sentence']);
$firstSentence[] = $parts[0];
}

$numberOfFirstSentences = count($firstSentence);

or $numberOfFirstSentences = mysql_num_rows($result);

this will result in an array of first sentences.
Is that what you are looking for? mcvdmvs
-- "It never hurts to help" -- Eek the Cat
 
I've adapted your code and written my own. By the way, $points looks something like this:

"Good location+Easy access to shops+New development"

Here's the code:
<?
$parts = explode(&quot;+&quot;,$points);
$first = count($parts);

print &quot;<ul>\n&quot;;
$i = 0;
while ($first != 0) {
print &quot;<li>$parts[$i]\n&quot;;
$first--;
}
print &quot;</ul>\n&quot;;
?>

But what I need it to do is display the next sentence. At the moment it just displays the first sentence 3 times.
 
That is because you forget to add the $i.
The following will work:

$points = &quot;Good location+Easy access to shops+New development&quot;;
$parts = explode(&quot;+&quot;,$points);
$rows = count($parts);

print &quot;<ul>\n&quot;;

$i = 0;
for ($i = 0; $i < $rows; $i++) {
print &quot;<li>$parts[$i]\n&quot;;
}
print &quot;</ul>\n&quot;; mcvdmvs
-- &quot;It never hurts to help&quot; -- Eek the Cat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top