Truncate string script

Truncate string script is used to truncate a long string to a particular length, and replace the truncated characters with a custom placeholder.

Demo :



Type Text Type Number  

Example :

Type Text :

PHP Hypertext Preprocessor is a web development dynamic web scripting programing language which can embedded into the HTML and interacts with databases.

Type Number :

25

if you need to truncate string function code you will copy and use to your website.


<?php
function truncateString($str, $maxChars=40, $holder="...")
{
  if (strlen($str) > $maxChars)
  {
    return trim(substr($str, 0, $maxChars)) . $holder;
  }
  else
  {
    return $str;
  }
}
$str = "PHP Hypertext Preprocessor is a web development dynamic web scripting programing language which can embedded into the HTML and interacts with databases".
echo truncateString($str,20, ">>>");
?>

In the above code is used to truncate a long sentance.Three parameters pass to truncateString() function. First arguments is $str. which is used to assign string. The second argument is used to truncate sentance. Here 20 is default.

Third argument is $holder. The strlen() function if greater than Max character substr will be execute otherwise string will be return.

<form id="tuncate_script" name="tuncate_script" method="post" action="">
<textarea id="tuncate_text" name="tuncate_text"></textarea>
<input type="text" id="number_rep" name="number_rep" />
<input type="submit" value="Cut"/>
</form>

In the above form is used to Truncate String Script.when click in the Cut button form will pass the string through POST method.

Free Download Script :

If you need to Truncate String Script click and download from following link.

Download




Content