Posted By

LuckyShot on 12/01/09


Tagged

wordpress replace trim


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

xmartyxcorex


Wordpress - Accurate Trim text function


 / Published in: PHP
 

Add to your functions.php in the active template.

Remove commented text. Fat/Thin char detection still not implemented. I'll update it as soon as I get some time. If you come with it please let me know :)

  1. function pcsite_trim ($text, $maxwords, $allowtags = "") {
  2. // <?php echo pcsite_trim(get_the_content(), 15, "");
  3. // Strip tags
  4. $text = strip_tags($text, $allowtags);
  5.  
  6. //workingonthis-$thinchars = ereg_replace("[^fijlt]", "", $text);
  7. //workingonthis-$fatchars = ereg_replace("[^mw]", "", $text);
  8. //workingonthis-$modifier = 5.1 + ( (strlen($thinchars) / strlen($text)) - (strlen($fatchars) / strlen($text)) );
  9. $modifier = 5.1; // average letters per word (in normal english it's 4.5, but in blogging and so it increases)
  10.  
  11. $text = substr($text, 0, $maxwords * $modifier);
  12.  
  13. $words = explode(" ", $text);
  14. $words = array_slice($words, 0, count($words) - 1 );
  15.  
  16. // Array to text
  17. $text = implode(" ", $words);
  18. $text = "<p>".$text."...</p>";
  19.  
  20. return $text;
  21. }

Report this snippet  

You need to login to post a comment.