|
24 Mar 2003, 17:18
|
#1
|
Forever Delayed
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
|
Summarising results in searches (PHP)
I did this, can anybody suggest a more elegant way to do it?
Code:
function snippet ($content)
{
$content = substr ($content, 0, 100);
$content = strrev ($content);
$end = strpos ($content, " ");
$end = strlen ($content) - $end;
$content = strrev ($content);
$content = substr ($content, 0, $end);
$content = $content." ...";
return $content;
}
M.
__________________
Firefly Oper and General l4m3r - "I Do Stuff"
O2 Rip-off campaign
<vampy> plus i hate people ... i despise humanity as a whole
pablissimo "I'm still geting over the fact you just posted a pic of your own vomit"
|
|
|
24 Mar 2003, 17:26
|
#2
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
Yeah, you should do it all on one line.
substr($content, strrchr(substr($content, 0, 100), " "))." ...";
Are you sure $content is over 100 chars and has a whitespace in? I don't see why splitting words is so b...
|
|
|
24 Mar 2003, 17:41
|
#3
|
/dev/zero Retired Mod
Join Date: May 2000
Posts: 415
|
I had a nice way of doing it using mysql functions (assuming the data was coming from a mysql query). Unfortunately, didnt end up using at the time, so it's on my home machine :/
__________________
#linux : Home of Genius
<idimmu> ok i was chained to a desk with this oriental dude
|
|
|
24 Mar 2003, 18:53
|
#4
|
Ngisne
Join Date: Jul 2001
Location: right here
Posts: 79
|
doesn't php have perl-like regexp support? in perl you could do
s/^(.{100,}?)\W.*$/$1.../
to keep everything up to the first non-word charcter after 100 characters and replace the rest with ...
__________________
down with signatures
|
|
|
24 Mar 2003, 20:07
|
#5
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
Not quite the same, as Mong's takes at most a hundred characters (when the 100th character is a space), while that PCRE takes at least 100 characters. So if there's supercajifrajiliciousexpialidocious starting at character one-hundred, it'll return a fair bit more than Mong's maximum of 104 chars. Though it has the added advantage of failing on strings under 100 chars.
|
|
|
25 Mar 2003, 10:35
|
#6
|
Registered User
Join Date: Apr 2002
Location: Leeds, but looking for a way to escape
Posts: 128
|
From the conext of the thread I am assuming that you are pulling the initial value of content from a DB.
Rather than getting your query to return the entire value in the table, it is possible to throttle the return result so that it only returns the first (or last) X characters.
If this query is going to be run many times, this method will save alot of unnecessary db usage.
__________________
SELECT everything FROM everywhere WHERE something = something_else;
> 42
|
|
|
25 Mar 2003, 18:58
|
#7
|
Forever Delayed
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
|
This query is run every time a user searches. The database is MySQL 3.23.52. I searched the MySQL docs for ways of limiting the amount returned, but can't find anything
M.
__________________
Firefly Oper and General l4m3r - "I Do Stuff"
O2 Rip-off campaign
<vampy> plus i hate people ... i despise humanity as a whole
pablissimo "I'm still geting over the fact you just posted a pic of your own vomit"
|
|
|
25 Mar 2003, 19:03
|
#8
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
Quote:
Originally posted by Mong
This query is run every time a user searches. The database is MySQL 3.23.52. I searched the MySQL docs for ways of limiting the amount returned, but can't find anything
M.
|
You just need a substring; substring(res,1,100) stylee.
|
|
|
|
All times are GMT +1. The time now is 04:11.
| |