Limit Displayed Characters From Your Text

This php script helps you limit displaying characters form you message and not cut out your word.

Syntax

substr($message, start, length);

Example 1

<?php

$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.

Example 2

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

############### Code

<?php

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

$message="You are now joining over 2000 current";

 

// Find what is the last character.
$post = substr($message,$position,1);


// In this step, if the last character is not " "(space) run this code.

// 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 no.20)

if($post !=" "){


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 "...";
?>


Similar Post

Get the width and height of an image using a function in PHP

PHP provides a function to get the width and height of an image. The getimagesize() function will determine the size of image file including flash file(swf).

Simple Ad Rotation Script

This is a very easy and simple ad rotation script, you can set percentage to show the ads and you can adapt this script to rotate anything.