strings.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /** Returns an array of email addresses **/
  59. function parseAddrs($text) {
  60. $text = str_replace(" ", "", $text);
  61. $text = str_replace(",", ";", $text);
  62. $array = explode(";", $text);
  63. return $array;
  64. }
  65. /** Returns a line of comma separated email addresses from an array **/
  66. function getLineOfAddrs($array) {
  67. $to_line = "";
  68. for ($i = 0; $i < count($array); $i++) {
  69. if ($to_line)
  70. $to_line = "$to_line, $array[$i]";
  71. else
  72. $to_line = "$array[$i]";
  73. }
  74. return $to_line;
  75. }
  76. ?>