strings.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. if (substr($haystack, -1) == $needle)
  19. $haystack = substr($haystack, 0, strlen($haystack) - 1);
  20. if (strrpos($haystack, $needle)) {
  21. $pos = strrpos($haystack, $needle) + 1;
  22. $data = substr($haystack, $pos, strlen($haystack));
  23. } else {
  24. $data = $haystack;
  25. }
  26. return $data;
  27. }
  28. // Wraps text at $wrap characters
  29. function wordWrap($passed, $wrap) {
  30. $passed = str_replace("&gt;", ">", $passed);
  31. $passed = str_replace("&lt;", "<", $passed);
  32. $words = explode(" ", trim($passed));
  33. $i = 0;
  34. $line_len = strlen($words[$i])+1;
  35. $line = "";
  36. while ($i < count($words)) {
  37. while ($line_len < $wrap) {
  38. $line = "$line$words[$i] ";
  39. $i++;
  40. $line_len = $line_len + strlen($words[$i])+1;
  41. }
  42. $line_len = strlen($words[$i])+1;
  43. if ($line_len < $wrap) {
  44. if ($i < count($words)) // don't <BR> the last line
  45. $line = "$line\n";
  46. } else {
  47. $endline = $words[$i];
  48. while ($line_len >= $wrap) {
  49. $bigline = substr($endline, 0, $wrap);
  50. $endline = substr($endline, $wrap, strlen($endline));
  51. $line_len = strlen($endline);
  52. $line = "$line$bigline<BR>";
  53. }
  54. $line = "$line$endline<BR>";
  55. $i++;
  56. }
  57. }
  58. $line = str_replace(">", "&gt;", $line);
  59. $line = str_replace("<", "&lt;", $line);
  60. return $line;
  61. }
  62. /** Returns an array of email addresses **/
  63. function parseAddrs($text) {
  64. if (trim($text) == "") {
  65. return;
  66. }
  67. $text = str_replace(" ", "", $text);
  68. $text = str_replace(",", ";", $text);
  69. $array = explode(";", $text);
  70. return $array;
  71. }
  72. /** Returns a line of comma separated email addresses from an array **/
  73. function getLineOfAddrs($array) {
  74. $to_line = "";
  75. for ($i = 0; $i < count($array); $i++) {
  76. if ($to_line)
  77. $to_line = "$to_line, $array[$i]";
  78. else
  79. $to_line = "$array[$i]";
  80. }
  81. return $to_line;
  82. }
  83. function translateText($body, $wrap_at) {
  84. /** Add any parsing you want to in here */
  85. $body = trim($body);
  86. $body_ary = explode("\n", $body);
  87. for ($i = 0; $i < count($body_ary); $i++) {
  88. $line = $body_ary[$i];
  89. $line = "^^$line";
  90. $line = str_replace(">", "&gt;", $line);
  91. $line = str_replace("<", "&lt;", $line);
  92. if (strlen($line) >= $wrap_at) // -2 because of the ^^ at the beginning
  93. $line = wordWrap($line, $wrap_at);
  94. $line = str_replace(" ", "&nbsp;", $line);
  95. $line = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $line);
  96. $line = nl2br($line);
  97. if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;&gt;") == 2) {
  98. $line = substr($line, 2, strlen($line));
  99. $line = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n";
  100. } else if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;") == 2) {
  101. $line = substr($line, 2, strlen($line));
  102. $line = "<TT><FONT COLOR=800000>$line</FONT></TT><BR>\n";
  103. } else {
  104. $line = substr($line, 2, strlen($line));
  105. $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\n";
  106. }
  107. $new_body[$i] = "$line";
  108. }
  109. $bdy = implode("\n", $new_body);
  110. return $bdy;
  111. }
  112. /* SquirrelMail version number -- DO NOT CHANGE */
  113. $version = "0.3pre1";
  114. function find_mailbox_name ($mailbox) {
  115. $mailbox = trim($mailbox);
  116. if (substr($mailbox, strlen($mailbox)-1, strlen($mailbox)) == "\"") {
  117. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  118. $pos = strrpos ($mailbox, "\"")+1;
  119. $box = substr($mailbox, $pos);
  120. } else {
  121. $box = substr($mailbox, strrpos($mailbox, " ")+1, strlen($mailbox));
  122. }
  123. return $box;
  124. }
  125. function replace_spaces ($string) {
  126. return str_replace(" ", "&nbsp;", $string);
  127. }
  128. function replace_escaped_spaces ($string) {
  129. return str_replace("&nbsp;", " ", $string);
  130. }
  131. ?>