Page 1 of 1

Limiting characters

Posted: Thu May 02, 2002 8:46 am
by nashyboy
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 :)

Re: Limiting characters

Posted: Thu May 02, 2002 8:54 am
by twigletmac
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 :D,
Mac

RE: Limiting Characters

Posted: Sat May 04, 2002 11:19 am
by nashyboy
I still can't get my head round this - is there any easier ways to do it or does anyone have any other examples?

:)

Posted: Sat May 04, 2002 11:29 am
by twigletmac
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

RE: Limiting Characters

Posted: Sat May 04, 2002 11:55 am
by nashyboy
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

Posted: Sat May 04, 2002 12:15 pm
by Bart
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:

Code: Select all

echo substr($firstname, 0, 6);
That will output the first 6 characters.

Posted: Sat May 04, 2002 12:19 pm
by hob_goblin
is there any way to add characters after something that is longer, without using alot of "if" statements?

Posted: Sat May 04, 2002 12:20 pm
by Bart
Didn't understand that question, please rephrase.

RE: Limiting Characters

Posted: Sat May 04, 2002 12:30 pm
by nashyboy
Perfect - Thanks a lot guys!!! :)

Posted: Sat May 04, 2002 12:33 pm
by dusty
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;
}
?>

Posted: Sat May 04, 2002 1:23 pm
by sam

Code: Select all

<? 
$text = "here is some long text"; 
echo ((strlen($text) > 5))?substr($text,0,5)."...":$text; 
?>
:mrgreen:

Cheers Moe

Posted: Sat May 04, 2002 1:32 pm
by pozytron
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