PHP Classes

File: tools/useful.php

Recommend this page to a friend!
  Classes of Alan H. Lake   I18N class   tools/useful.php   Download  
File: tools/useful.php
Role: Auxiliary script
Content type: text/plain
Description: Functions used by other code
Class: I18N class
Get translation texts from different containers
Author: By
Last change: Added subdirectory to file name.
Date: 18 years ago
Size: 11,090 bytes
 

Contents

Class file image Download
<?php /* useful.php - Useful generic functions for PHP Copyright (C) 2000 Jacob Moorman This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Current version: 0.10 (21 September, 2000) This file contains the following functions: BetterStrTok ($delimiter, $string="") ConvertSpacesToNonBreakingSpaces ($string, $tagexception="") URLs_Clickable ($string) WordWrapString ($str, $width="75", $break="\n", $cut="0") */ /* -----------------------------------------------------------------------*/ function BetterStrTok ($delimiter, $string="") { /* PHP's strtok() function sucks, simply put. This strtok()'ish function may be of more use. Specified delimiter is returned if there are no more tokens to be had. In the event that the delimiter appears twice in a row, an empty token will be returned. */ // Make this variable persistent static $pos, $stringval; if (!empty ($string)) { $pos = 0; $stringval = $string; } else { if (empty ($stringval)) return $delimiter; } // Our position is past the end of the string, so it is time to stop. if ($pos >= strlen ($stringval)) { return $delimiter; } // Get the rest of the string $string = substr ($stringval, $pos); if ($delimiter == substr ($string, 0, strlen ($delimiter))) { // The delimiter is found at the beginning of the string, so // return an empty string $pos = $pos + strlen ($delimiter); return ""; } $hit = strpos ($string, $delimiter); if ($hit > 0) { // If we can find the delimiter in the remaining string, return // the next token $string = substr ($stringval, $pos, $hit); $pos = $hit + $pos + strlen ($delimiter); return $string; } else { // Otherwise, return the remaining string (last token in string) $pos = strlen ($stringval) + 1; return $string; } } /* ---------------------------------------------------------------------- */ function ConvertSpacesToNonBreakingSpaces ($string, $tagexception="") { // Convert spaces to &nbsp; in string. If $tagexception, then do // not convert spaces which appear to be inside HTML tags. // If not $tagexception, then we can do this the easy way. if ($tagexception != "1") return str_replace (" ", "&nbsp;", $string); // Otherwise, we need to make sure the spaces are not inside HTML tags. $remainingstring = $string; while (strlen ($remainingstring) > 0) { $nextlt = strpos ($remainingstring, "<"); $nextgt = strpos ($remainingstring, ">"); $nextspace = strpos ($remainingstring, " "); // There are no spaces left, so return the remainder if (!is_integer ($nextspace)) { $processedstring .= $remainingstring; return $processedstring; } // There are no more HTML tags left, so finish conversion and return if (!is_integer ($nextlt)) { $processedstring .= str_replace (" ", "&nbsp;", $remainingstring); return $processedstring; } if ($nextspace < $nextlt) { $workstring = substr ($remainingstring, 0, $nextlt); $processedstring .= str_replace (" ", "&nbsp;", $workstring); $remainingstring = substr ($remainingstring, strlen ($workstring)); } else { $workstring = substr ($remainingstring, 0, $nextgt + 1); $processedstring .= $workstring; $remainingstring = substr ($remainingstring, strlen ($workstring)); } } return $processedstring; } /* -----------------------------------------------------------------------*/ function DisplaySelect ($values, $selectname) { /* Based on an array (of key/value pairs), display an HTML SELECT holding the same properties; sorted by values */ // Sort the values in ascending order asort ($values); reset ($values); // Send out the select tag echo "<SELECT NAME=$selectname>"; while (list ($key, $value) = each ($values)) { // Send out each option echo "<OPTION VALUE=$key>$value\n"; } echo "</SELECT>"; } /* -----------------------------------------------------------------------*/ function URLs_Clickable ($string) { $processedstring = ereg_replace ( "http://(([A-Za-z0-9+-\/\#-\&\!\:\=\?\@\_\~])*)." . "([a-zA-Z0-9\/\+])", "<a href=\"\\0\">\\0</a>", $string); return $processedstring; } /* -----------------------------------------------------------------------*/ function WordWrapString ($str, $width="75", $break="\n", $cut="0") { // This function is named somewhat redundantly, so it doesn't conflict // with the WordWrap() function included in PHP4. Since this function // is not available in the PHP tree, this should be useful. // Wrap string $str at $width (default 75) using $break as the // replacement character where the line is broken. If $cut, then // we should perform a hard word wrap in cases where a word exceeds the // specified $width (i.e. the word will be split). Since the code to // this function is not derived from the PHP4 equivalent, the resulting // wrapped text may differ. // Set our initial position to the beginning of the string $pos = 0; // $pos is used to walk the original string $lastwhitespace = $lastbreak = $pos; // $lastwhitespace shows position of last found whitespace in original // string; $lastbreak shows position of last break in original string $workstring = $remainingstring = $str; // While our position is still within our string bounds while (strlen ($remainingstring) > 0) { // Find our next linebreak $nextbreak = strpos ($workstring, "\n"); // If our linebreak occurs before we would need to wrap, // make the break there. if ((is_integer ($nextbreak)) and ($nextbreak <= $width)) { // Tack this bit onto our processed (wrapped) string $processedstr .= substr ($remainingstring, 0, $nextbreak) . $break; // Update our position $pos = $nextbreak + 1; } elseif (strlen ($remainingstring) <= $width) { $processedstr .= $remainingstring; $pos = strlen ($remainingstring) + 1; } else { // Otherwise, we need to see if there is whitespace between // our starting position and the next linebreak and see // if it is close enough that we don't need to deal with $cut $still_looking = 1; while ($still_looking == "1") { // Find our last whitespace $lastspace = strrpos ($workstring, " "); $lasttab = strrpos ($workstring, "\t"); if ($lastspace > $lasttab) $lastwhitespace = $lastspace; else $lastwhitespace = $lastspace; if ((is_integer ($lastwhitespace)) and ($lastwhitespace > $width)) { // If we find a whitespace but it is past our // width point, we need to keep looking. $workstring = substr ($workstring, 0, $lastspace); } elseif (is_integer ($lastwhitespace)) { // This should be a valid breaking point // Tack this bit onto our processed (wrapped) string $processedstr .= substr ($workstring, 0, $lastwhitespace) . $break; // Update our position $pos = $lastwhitespace + 1; $still_looking = 0; } else { // Otherwise, we are in the position of having to // deal with a $cut scenario (we cannot break and // the remaining string is too long if ($cut == "1") { // We are using 'cut' mode, so chop on the // dotted line. $processedstr .= substr ($remainingstring, 0, $width) . $break; // Update our position $pos = $width + 1; $still_looking = 0; } else { // Not using 'cut' mode, so take everything // we are left with. $processedstr .= $remainingstring . $break; // Update our position $pos = strlen ($remainingstring) + 1; $still_looking = 0; } } } } // Get the remaining string $remainingstring = substr ($remainingstring, $pos); $workstring = $remainingstring; } return $processedstr; } ?>