"Philip J. Newman" <[EMAIL PROTECTED]> wrote in message
007a01c19fbe$2fbfb4e0$0401a8c0@philip">news:007a01c19fbe$2fbfb4e0$0401a8c0@philip...
> Would like to know if there is away that I can only print out the 1st 100
charactors that are pulled from a mySql Database, anyone help?
>
> Philip J. Newman
> Philip's Domain - Internet Project.
> http://www.philipsdomain.com/
> [EMAIL PROTECTED]
> Phone: +64 25 6144012

Two ways:
1. SELECT LEFT(fieldname, 100) .... FROM table ...
[see: http://www.mysql.com/doc/S/t/String_functions.html ]
This will make MySQL return only the first 100 characters from your table.
It SHOULD be safe in instances where there are less than 100 characters as
well (it'll return the entire string in that case)
This is the faster approach as the work is done in MySQL, and only 100 bytes
of data are actually sent to your application (as opposed to the entire
field). It has some limitations, however.

2. $shortened_string = substr($string, 0, 100);
[see: http://www.php.net/substr/ ]

Be warned that if $string contains HTML escapes, entities will count as
multiple character, e.g. &amp; is 5 characters and thus may be cut in half
if it occurs towards the end of your 100 characters. See
http://www.php.net/get_html_translation_table for a possible solution to
this problem, or store your data in plaintext (this is impossible if you
need to store HTML formatting in your text).

I use this function to trim long names on my message boards at
http://venura.net/ . Mind you, it will not properly handle _all_ HTML
escapes, but it does the job well enough.

// strLimit($string, $length)
// Limits string lengths. If a string is shorter than length, returns that
string. If the string is longer than length, returns the first
// (length-3) characters of the string, plus a '...' to show continuation.
Use: lists.
// Modified to appropriately handle html-escapes
function strLimit($string, $length) {
    $length += (4 * substr_count($string, "&")); // comment out this line if
you aren't dealing with HTML escapes
    if(strlen($string) <= $length) return $string;
    else return substr($string, 0, $length - 3) . "...";
}

One possible trick you can do in order to boost performance (by not
transferring the entire string from the entire database, but still being
able to know whether it's too long) is combine both #1 and #2 and actually
pull the first 150-200 characters from the database, which should be enough
to deal with most HTML escapes without dumping the entire table.

-- Daniel Grace <[EMAIL PROTECTED]>




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to