Wednesday, June 8, 2011

Limit Displayed Characters From Your Text

This php tutorial will help you that how will apply limit displaying characters form you message and not cut out your word.


Syntax


substr($message, start, length);


Example 1


{code type=codetype}

<?

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

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

echo $post;
echo "...";

?>


This result shows 14 characters from your message

"Limit Display..."
{/code}

This is not good. We want to display "Limit Displayed Characters..." 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 displaying.
3. If the last character displaying is not " " (space) then go to next character until we found it.
4. Display your message.


|---|--------------------- code --------------------|---|


{code type=codetype}

<?
$position=14; // Define here that how many character you want to display there.

$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 here
echo $post;
echo "...";
?>


{/code}

No comments:

Post a Comment