Replacing a long string with special characters like dots.


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. //Storing a long string in a variable.
  3. $string = "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.";
  4.  
  5. //Display the string in it's original format.
  6. echo $string;
  7.  
  8. //Get the lenght of the string.
  9. $str_length = strlen($string);
  10. echo "<br/>";
  11.  
  12. //Method to chunk and replace a long string with some characters if you have space issues.
  13. if($str_length > 25)
  14. {
  15. //displayig the replaced string.
  16. echo substr($string, 0, 25) . "..." . "<br/>";//Replacing the string with Dots.
  17. echo substr($string, 0, 25) . "***" . "<br/>";//Replacing the string with Asteriks.
  18. echo substr($string, 0, 25) . ">>>" . "<br/>";//Replacing the string with Angular Brackets.
  19. }
  20. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.