# php - limit text display on web page



## peterjcs (Jan 23, 2007)

i would like to know how to limit the number of character display on web page. for example, i retrieve the text from the database, and display on the web page. But since there are lots of text in the database, i only want the first 100 character to be display on the web page. How to do it using PHP?

Thank you


----------



## Emporer_D (Jul 5, 2005)

I found this after doing a little searching:

________________________________________________________
<?

$position=14; // Define how many character you want to display.

$message="You are now joining over 2000 current"; 
$post = substr($message, 0, $position); 

echo $post;
echo "..."; 

?>

This result shows 14 characters from your message

"You are now jo..." 

This is not good. We want to display "You are now joining..." 
Let's solve this problem in example 2.

____________________________________________________________


1. Define how many characters you want to display.
2. Find what is the last character displaying. 
3. If the last character displaying is not " " (space) then go to next character until we found it.
4. Display your message. 

############### Code 
<?
$position=14; // Define how many characters you want to display.

$message="You are now joining over 2000 current"; 
$post = substr($message,$position,1); // Find what is the last character displaying. We find it by getting only last one character from your display message. 

if($post !=" "){ // In this step, if last character is not " "(space) do this step . 

// Find until we found that last character is " "(space) 
// by $position+1 (14+1=15, 15+1=16 until we found " "(space) that mean character 20) 
while($post !=" "){
$i=1;
$position=$position+$i; 

$message="You are now joining over 2000 current"; 
$post = substr($message,$position,1); 
}

}

$post = substr($message,0,$position); // Display your message
echo $post;
echo "...";
?>


----------



## peterjcs (Jan 23, 2007)

Thank a lot Emporer_D. I appreciate it.


----------



## darryncooke (Jul 24, 2009)

i hate to revive an old thread like this but this is what i need and i am having a slight problem. I am calling a field from a dbase that I am trying to have the character amount limited.

<?
$position=8; // Define how many characters you want to display.

$message=echo $row_articles_select['title'];
$post = substr($message,$position,1); // Find what is the last character displaying. We find it by getting only last one character from your display message.

if($post !=" "){ // In this step, if last character is not " "(space) do this step .

// Find until we found that last character is " "(space)
// by $position+1 (14+1=15, 15+1=16 until we found " "(space) that mean character 20)
while($post !=" "){
$i=1;
$position=$position+$i;

$message=echo $row_articles_select['title'];
$post = substr($message,$position,1);
}

}

$post = substr($message,0,$position); // Display your message
echo $post;
echo "...";
?>

and its not quite working.


----------



## FredT (Nov 16, 2007)

Take out the echo on this line (occurs twice):

$message=echo $row_articles_select['title'];


----------

