strings.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?
  2. //*************************************************************************
  3. // Count the number of occurances of $needle are in $haystack.
  4. //*************************************************************************
  5. function countCharInString($haystack, $needle) {
  6. $len = strlen($haystack);
  7. for ($i = 0; $i < $len; $i++) {
  8. if ($haystack[$i] == $needle)
  9. $count++;
  10. }
  11. return $count;
  12. }
  13. //*************************************************************************
  14. // Read from the back of $haystack until $needle is found, or the begining
  15. // of the $haystack is reached.
  16. //*************************************************************************
  17. function readShortMailboxName($haystack, $needle) {
  18. $len = strlen($haystack);
  19. for ($i = $len - 1; ($i >= 0) && (!$found);$i--) {
  20. $char = $haystack[$i];
  21. if ($char == $needle)
  22. $found = 1;
  23. else
  24. $data .= $char;
  25. }
  26. return strrev($data);
  27. }
  28. // Wraps text at $wrap characters
  29. function wordWrap($passed, $wrap) {
  30. $words = explode(" ", trim($passed));
  31. $i = 0;
  32. $line_len = strlen($words[$i])+1;
  33. $line = "";
  34. while ($i < count($words)) {
  35. while ($line_len < $wrap) {
  36. $line = "$line$words[$i]&nbsp;";
  37. $i++;
  38. $line_len = $line_len + strlen($words[$i])+1;
  39. }
  40. $line_len = strlen($words[$i])+1;
  41. if ($line_len < $wrap) {
  42. if ($i < count($words)) // don't <BR> the last line
  43. $line = "$line<BR>";
  44. } else {
  45. $endline = $words[$i];
  46. while ($line_len >= $wrap) {
  47. $bigline = substr($endline, 0, $wrap);
  48. $endline = substr($endline, $wrap, strlen($endline));
  49. $line_len = strlen($endline);
  50. $line = "$line$bigline<BR>";
  51. }
  52. $line = "$line$endline<BR>";
  53. $i++;
  54. }
  55. }
  56. return $line;
  57. }
  58. ?>