User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion
Register FAQ Members List Calendar Arcade Today's Posts

Reply
Thread Tools Display Modes
Unread 24 Mar 2003, 17:18   #1
Mong
Forever Delayed
 
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
Mong is on a distinguished road
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"
Mong is offline   Reply With Quote
Unread 24 Mar 2003, 17:26   #2
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
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...
queball is offline   Reply With Quote
Unread 24 Mar 2003, 17:41   #3
MT
/dev/zero
Retired Mod
 
MT's Avatar
 
Join Date: May 2000
Posts: 415
MT is an unknown quantity at this point
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
MT is offline   Reply With Quote
Unread 24 Mar 2003, 18:53   #4
Atamur
Ngisne
 
Join Date: Jul 2001
Location: right here
Posts: 79
Atamur is an unknown quantity at this point
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
Atamur is offline   Reply With Quote
Unread 24 Mar 2003, 20:07   #5
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
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.
queball is offline   Reply With Quote
Unread 25 Mar 2003, 10:35   #6
BuddhistPunk
Registered User
 
Join Date: Apr 2002
Location: Leeds, but looking for a way to escape
Posts: 128
BuddhistPunk is an unknown quantity at this point
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
BuddhistPunk is offline   Reply With Quote
Unread 25 Mar 2003, 18:58   #7
Mong
Forever Delayed
 
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
Mong is on a distinguished road
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"
Mong is offline   Reply With Quote
Unread 25 Mar 2003, 19:03   #8
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
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.
queball is offline   Reply With Quote
Reply



Forum Jump


All times are GMT +1. The time now is 07:31.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018