Posted By

xzkcz on 07/03/09


Tagged

substr


Versions (?)

mySubstr


 / Published in: PHP
 

  1. function mysubstr($string,$length,$dots=false,$wc_size=2) {
  2. $words = 0;
  3. $n = 0;
  4. if(strlen($string)<$length){
  5. return $string;
  6. }
  7. while ($n<strlen($string) && $words<$length ) {
  8. $t = ord($string[$n]);
  9. if($t==9 || $t==10 || (32<=$t && $t<=127)) {
  10. $n++;
  11. $words ++;
  12. } elseif(192<=$t && $t<=223) {
  13. $n += 2;
  14. if($wc_size == 1){
  15. $words += 1;
  16. }else{
  17. $words += 2;
  18. }
  19. } elseif(224<=$t && $t<=239) {
  20. $n += 3;
  21. if($wc_size == 1){
  22. $words += 1;
  23. }elseif($wc_size == 2){
  24. $words += 2;
  25. }else{
  26. $words += 3;
  27. }
  28. } else {
  29. $n++;
  30. $words ++;
  31. }
  32. }
  33. $wordscut = substr($string, 0, $n);
  34. if(strlen($wordscut)<strlen($string) && $dots){
  35. $wordscut .= '...';
  36. }
  37. return $wordscut;
  38. }

Report this snippet  

You need to login to post a comment.