You are here: Re: [PHP] Substr by words « PHP « IT news, forums, messages
Re: [PHP] Substr by words

Posted by "Richard Lynch" on 10/29/05 22:37

On Sat, October 29, 2005 6:36 am, Danny wrote:
> I need to extract 50 words more or less from a description field. How
> can i
> do that?. Substr, cuts the words. Is there any other way to that,
> without
> using and array? I mean and implemented function in PHP 4.x
> I´ve been googling around, but wordwrap, and substr is driving me
> mad...

Keep in mind that the "pipe" between your database and PHP is a rather
small narrow expensive opening.

Sucking down your ENTIRE description field to throw away all but 50
characters may not be the best use of limited resources.

[This is all MOOT if you have no dreams of your site being "big" some
day.]

You therefore may want to consider something like:

select substring(description, 1, instr(description, ' ', 50)) as
description_50, substring(description, instr(description, ' ', 50), 1)
as more from ...

$description_50 will be 50 chars, more or less
$more will tell you if there was "more" or not

You will only be getting ~50 characters squeezed through that narrow
expensive db <-> PHP pipeline.

I believe that in MOST PHP/database applications this is going to be a
better performing solution, and it's somewhat "cleaner" aesthetically
than shoveling a bunch of data around that you're going to discard
anyway.

* instr may or may not be the right function in your database. I
always forget the name of this one and have to look it up.

--
Like Music?
http://l-i-e.com/artists.htm

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация