Page 1 of 1
Number of characters
Posted: Sun May 09, 2010 4:16 am
by tito85
Hi,
I am using this code to display a movie title, however I would like that I limit the number of characters that can be diplayed.
For example: "The Chronicles of Narnia: The Lion, the Witch and the Wardrobe" has about 50 characters. I would like to display only the first 10 characters, ie: "The Chroni" and then if possible add like 3 dots (...) that means the title is not all shown.
Any help on how can I do this?
Code: Select all
echo "<center><a href=\"moviedetails.php?id=" . $movie['MovieID'] . "\">" . $movie['Title'] . "</a></center>";
Re: Number of characters
Posted: Sun May 09, 2010 7:43 am
by Zyxist
You need these two functions: strlen() and substr(). The first one gives you the length of a string, the second one - cuts a piece of a string and returns it. You simply check the title length and if it is longer than 10 characters, you cut the first 10 characters and append three dots to it.
Re: Number of characters
Posted: Mon May 10, 2010 1:32 am
by tito85
Can you help by an example please? Sorry but i'm a bit new into PHP...
Thanks for you help!
Re: Number of characters
Posted: Mon May 10, 2010 3:50 am
by jraede
You can automatically display the first 10 characters of any string by doing the following:
Code: Select all
$shorter_string = substr($full_string, 0, 10);
The 0 is the starting position, and the 10 is the ending position. So, it takes characters 0 thru 10 of string $full_string.
Re: Number of characters
Posted: Mon May 10, 2010 6:31 am
by tito85
Tnx but how should I use this string with the string i mentioned?
Re: Number of characters
Posted: Mon May 10, 2010 7:51 am
by hypedupdawg
Assuming that you are still using the $movie array, I should think this is appropriate:
Code: Select all
<?php
function shortTitle($title)
{
$short = substr($title, 0, 10);
return $short;
}
echo "<center><a href=\"moviedetails.php?id=" . $movie['MovieID'] . "\">" . shortTitle($movie['Title']) . "...</a></center>";
?>
You should then reference each title to the "shortTitle" function, so that if you change the "10" value, you can change all the lengths of the titles.
Re: Number of characters
Posted: Mon May 10, 2010 12:48 pm
by tito85
Hi,
Thanks for your help. It is working but only for one record. The query i have can get multiple records and this is only working on the first records then it displays;
Fatal error: Cannot redeclare shortTitle() (previously declared in C:\wamp\www\movie\comingsoon.php:41)
Any idea why?
Re: Number of characters
Posted: Mon May 10, 2010 1:23 pm
by AbraCadaver
tito85 wrote:Hi,
Thanks for your help. It is working but only for one record. The query i have can get multiple records and this is only working on the first records then it displays;
Fatal error: Cannot redeclare shortTitle() (previously declared in C:\wamp\www\movie\comingsoon.php:41)
Any idea why?
You can only define a function once. In a loop it tries to define it every loop iteration. Until you're more familiar with programming, I would just use:
Code: Select all
echo "<center><a href=\"moviedetails.php?id=" . $movie['MovieID'] . "\">" . substr($movie['Title'], 0, 10) . "...</a></center>";
Re: Number of characters
Posted: Mon May 10, 2010 1:42 pm
by tito85
Tnx man it is working as i wanted!