Number of characters

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

Post Reply
tito85
Forum Contributor
Posts: 104
Joined: Sat Mar 13, 2010 11:26 am

Number of characters

Post 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>";
User avatar
Zyxist
Forum Contributor
Posts: 104
Joined: Sun Jan 14, 2007 10:44 am
Location: Cracow, Poland

Re: Number of characters

Post 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.
tito85
Forum Contributor
Posts: 104
Joined: Sat Mar 13, 2010 11:26 am

Re: Number of characters

Post by tito85 »

Can you help by an example please? Sorry but i'm a bit new into PHP...

Thanks for you help!
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: Number of characters

Post 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.
tito85
Forum Contributor
Posts: 104
Joined: Sat Mar 13, 2010 11:26 am

Re: Number of characters

Post by tito85 »

Tnx but how should I use this string with the string i mentioned?
User avatar
hypedupdawg
Forum Commoner
Posts: 74
Joined: Sat Apr 10, 2010 5:21 am

Re: Number of characters

Post 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.
tito85
Forum Contributor
Posts: 104
Joined: Sat Mar 13, 2010 11:26 am

Re: Number of characters

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Number of characters

Post 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>";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
tito85
Forum Contributor
Posts: 104
Joined: Sat Mar 13, 2010 11:26 am

Re: Number of characters

Post by tito85 »

Tnx man it is working as i wanted!
Post Reply