PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
nashyboy
Forum Newbie
Posts: 14 Joined: Thu May 02, 2002 8:46 am
Post
by nashyboy » Thu May 02, 2002 8:46 am
can some one tell me how to do the following - its probably easy but ....
basically when displaying eg, firstname's from a table - i want to limit the number of characters displayed - say to 6 characters
therefore someone with a long firstname like Alexandra
would appear next to the other results like so
paul
mark
alexan
if anyone knows what i mean here - any help would be appreciated.
Cheers
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Thu May 02, 2002 8:54 am
nashyboy wrote: i want to limit the number of characters displayed - say to 6 characters
You can use the
substr() function to do this.
Code: Select all
$firstname1 = 'Alexandra';
$firstname2 = 'Bob';
echo substr($firstname1, 0. 6);
echo '<br />'
echo substr($firstname2, 0, 6);
This outputs:
Alexan
Bob
Nice and easy
,
Mac
nashyboy
Forum Newbie
Posts: 14 Joined: Thu May 02, 2002 8:46 am
Post
by nashyboy » Sat May 04, 2002 11:19 am
I still can't get my head round this - is there any easier ways to do it or does anyone have any other examples?
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Sat May 04, 2002 11:29 am
AFAIK there isn't an easier way of doing it. Is there any particular part that you find confusing
? Also, have you read the manual entry on substr() as there's a quite a few examples in there.
http://www.php.net/manual/en/function.substr.php
Mac
nashyboy
Forum Newbie
Posts: 14 Joined: Thu May 02, 2002 8:46 am
Post
by nashyboy » Sat May 04, 2002 11:55 am
Basically if this is the code below - where does what go?
I've looked on php.net and find it confusing - find it easier to understand when seeing full examples.
from the below how would i make it so that when displaying firstnames, even though some are longer than others, it only displays the first 6 charachters of each name?
<?
$connection = mysql_connect("localhost","root")
or die ("Unable to connect to MySQL server.");
$db = mysql_select_db("mydb", $connection) or die ("Unable to select database.");
$sql = "SELECT * FROM people";
$sql_result = mysql_query($sql,$connection) or die ("Couldn't execute SQLquery");
echo "";
while ($row = mysql_fetch_array($sql_result))
{
$id = $row["id"];
$firstname = $row["firstname"];
$surname = $row["surname"];
echo "
<table width=100% border=0 cellspacing=0 cellpadding=0>
<tr>
<td>
<font face=Verdana, Arial, Helvetica, sans-serif size=2>
<b>
$firstname
</b>
</font>
</td>
</tr>
</table>";
}
echo "";
mysql_free_result($sql_result);
mysql_close($connection);
?>
I appreciate any help you can give me
Cheers
Bart
Forum Newbie
Posts: 9 Joined: Sat May 04, 2002 12:12 pm
Location: Ontario Canada
Post
by Bart » Sat May 04, 2002 12:15 pm
Ok, this is simple, I don't see how you missed twiglemacs explanation
This line:
echo "
<table width=100% border=0 cellspacing=0 cellpadding=0>
<tr>
<td>
<font face=Verdana, Arial, Helvetica, sans-serif size=2>
<b>
$firstname
</b>
</font>
</td>
</tr>
</table>";
All you have to do is change the line containing $firstname to the following:
That will output the first 6 characters.
hob_goblin
Forum Regular
Posts: 978 Joined: Sun Apr 28, 2002 9:53 pm
Contact:
Post
by hob_goblin » Sat May 04, 2002 12:19 pm
is there any way to add characters after something that is longer, without using alot of "if" statements?
Bart
Forum Newbie
Posts: 9 Joined: Sat May 04, 2002 12:12 pm
Location: Ontario Canada
Post
by Bart » Sat May 04, 2002 12:20 pm
Didn't understand that question, please rephrase.
nashyboy
Forum Newbie
Posts: 14 Joined: Thu May 02, 2002 8:46 am
Post
by nashyboy » Sat May 04, 2002 12:30 pm
Perfect - Thanks a lot guys!!!
dusty
Forum Contributor
Posts: 122 Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA
Post
by dusty » Sat May 04, 2002 12:33 pm
if you mean something like cutting text off and replacing with "..." like a run on:
Code: Select all
<?
$text = "here is some long text.";
echo substr($text,0,5) . "...";
?>
and if you need an if statement to make sure the text is longer than 5 characters before adding the "...".
Code: Select all
<?
$text = "here is some long text";
if(strlen($text) > 5) {
echo substr($text,0,5) . "...";
} else {
echo $text;
}
?>
sam
Forum Contributor
Posts: 217 Joined: Thu Apr 18, 2002 11:11 pm
Location: Northern California
Contact:
Post
by sam » Sat May 04, 2002 1:23 pm
Code: Select all
<?
$text = "here is some long text";
echo ((strlen($text) > 5))?substr($text,0,5)."...":$text;
?>
Cheers Moe
pozytron
Forum Newbie
Posts: 12 Joined: Fri May 03, 2002 7:14 pm
Location: Denver
Post
by pozytron » Sat May 04, 2002 1:32 pm
To bank off of sam's post, I have had instances in my many years of programming where I need
exactly 255 characters, or less (or some other number of characters. In that case, use
Code: Select all
echo ((strlen($text) > 255)) ? substr($text,0,252)."..." : $text;
This auto-magically will make the text a maximum of 255 chars and still add the "...".
Just thought I might share.
Cheers