473,320 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

displaying first 50 characters of a mysql text field?

Hi all

I have been searching for hours on the php website for a function that
will allow me to display just the first 50 chars of a mysql text field
in a results table, but i'm having no luck. I need it to create a
results page for a site i'm creating in php. Can anyone help with this
problem?

Kind regards

Marc

Oct 23 '05 #1
4 8256
phpuser32423 wrote:
Hi all

I have been searching for hours on the php website for a function that
will allow me to display just the first 50 chars of a mysql text field
in a results table, but i'm having no luck. I need it to create a
results page for a site i'm creating in php. Can anyone help with this
problem?

Kind regards

Marc


$str50 = substr($mystring, 0, 50);

-david-

Oct 23 '05 #2
I noticed that Message-ID: <0d***********@fe24.usenetserver.com> from
David Haynes contained the following:

$str50 = substr($mystring, 0, 50);


I use this method in a function and if the text is truncated the
function adds '...more' at the end. The function also takes a url so
that the word 'more' can be a link to an expanded version. $length is
obviously the amount you to which you want to truncate. The function
also 'steps back' to avoid truncating in the middle of a word.

<?php
function truncate($value,$length,$url){
if(strlen($value)>$length){
$value=substr($value,0,$length);
$n=0;
while(substr($value,-1)!=chr(32)){
$n++;
$value=substr($value,0,$length-$n);
}
$value=$value." ...<a href='$url'><em>more</em></a>";
}
return $value;
}
//usage
echo truncate("the quick brown fox jumps over the lazy
dog",15,"http:www.example.com?id=someid");
?>
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Oct 23 '05 #3
thanks guys thats a great help

Oct 23 '05 #4
phpuser32423 wrote:
Hi all

I have been searching for hours on the php website for a function that
will allow me to display just the first 50 chars of a mysql text field
in a results table, but i'm having no luck. I need it to create a
results page for a site i'm creating in php. Can anyone help with this
problem?

Kind regards

Marc


If you only need the first 50 chars for that field then you can get
MySQL to do the work for you ...

MySQL - Substring: Extracting part of a string

SELECT name, SUBSTRING(name,0,50) FROM cia WHERE region = 'Asia'

MySQL docs... http://dev.mysql.com/doc/refman/5.0/...functions.html

Craig
Oct 24 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Mosher | last post by:
Hi all, I have an issue with php and/or mysql. I have a php form that writes "items" to a mysql database, including a description of the item. On the mysql server, "magic_quotes_gpc" is ON. I...
0
by: Geoffrey | last post by:
I'm trying to use an SQL plug-in to synch. FileMaker (FM) with a MySQL DB. All is going great EXCEPT my FM text fields. If I have any special characters in my FM text field, they foul-up the...
2
by: Andrew | last post by:
Hi, I have a form set up which collects details and then when submitted it creates a new record in a table called rbh_vacancies Most of the time it adds the records perfectly, but there seems...
4
by: Ewok | last post by:
let me just say. it's not by choice but im dealing with a .net web app (top down approach with VB and a MySQL database) sigh..... Anyhow, I've just about got all the kinks worked out but I am...
4
by: Kivanc Toker | last post by:
Hi, I've got a problem with displaying texts with turkish characters. There is a website administration system, which is developed using ASP (VBScript) and ADO. This system is being used for...
2
by: donpro | last post by:
Hi, I have a varchar field in a MySQL database that contains a line of text like so: "This is a line if text" The double quotes are included in the database field. I cannot seem to...
2
by: Brave | last post by:
I'm hoping someone can help me with a small issue. I have an asp page that displays informaton from an Access database. I want to create a form that allows users to display only data that...
4
by: Bob | last post by:
Hi all, I've got a table that I've imported and it has junk at the top of the table, so after import I run a delete query to remove the junk lines then I'm left with the field names I want for...
2
by: matech | last post by:
I have a problem with uploading special characters from excel files to mysql 5. It doesn't matter if I use UTF-8 or iso-8859-1 when uploading the trademark ™ symbol. htmlspecialchars() or...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.