core
[ class tree: core ] [ index: core ] [ all elements ]

Source for file Strings.class.php

Documentation is available at Strings.class.php

  1. <?php
  2. /**
  3.  * For including this file you have to define the constant "CLASSPATH".
  4.  * Because every include in the framework depends on the CLASSPATH definition.
  5.  * The CLASSPATH means the relative path to the folder that contains the
  6.  * framework GilliGan.
  7.  *
  8.  * @package core
  9.  */
  10. if( !defined( "CLASSPATH" ) )
  11. {
  12.   echo "<h3>You have to define the constant CLASSPATH!</h3>\n";
  13.   echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\n";
  14.   exit();
  15. }
  16.  
  17. /**
  18.  * Loading the class to manipulate arrays.
  19.  */
  20. include_once( CLASSPATH."core/Arrays.class.php" );
  21.  
  22. /**
  23.  * Static methods
  24.  *
  25.  * @static
  26.  * @package   core
  27.  * @version   0.1.96
  28.  *
  29.  * @author    Daniel Plücken <daniel@debakel.net>
  30.  *
  31.  * @license   http://www.gnu.org/copyleft/lesser.html
  32.  *             GNU Lesser General Public License
  33.  * @copyright Copyright (c) 2003 Daniel Plücken <daniel@debakel.net>
  34.  *
  35.  *  This library is free software; you can redistribute it and/or
  36.  *  modify it under the terms of the GNU Lesser General Public
  37.  *  License as published by the Free Software Foundation; either
  38.  *  version 2.1 of the License.
  39.  *
  40.  *  This library is distributed in the hope that it will be useful,
  41.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  42.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  43.  *  GNU Lesser General Public License for more details.
  44.  *
  45.  *  You should have received a copy of the GNU Lesser General
  46.  *  Public License along with this library; if not, write to the
  47.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  48.  *  Boston, MA 02111-1307 USA
  49.  */
  50. class Strings extends ABSTObject
  51. {
  52.    /**
  53.     * Functions like strpos( string haystack, string needle [, int offset] ),
  54.     * but you can use a regular expression as needle.
  55.     * BEWARE: lookahead, lookbehind and all chars itentified by their position
  56.     *         will NOT function, because of the intern relay to the function
  57.     *         strpos!
  58.     *
  59.     * @version 1.0
  60.     * @since   0.1.0
  61.     * @access  public
  62.     *
  63.     * @static
  64.     *
  65.     * @param   string  $haystack 
  66.     * @param   string  $needle 
  67.     * @param   string  $offset 
  68.     *
  69.     * @return  integer 
  70.     */
  71.    function pregpos( $haystack, $needle, $offset = 0 )
  72.    {
  73.       $haystack = substr( $haystack, $offset );
  74.  
  75.       $tmp_match_arr = array();
  76.       if ( preg_match( $needle, $haystack, $tmp_match_arr ) )
  77.          $pos = strpos( $haystack, $tmp_match_arr[0] );
  78.       else
  79.          return false;
  80.  
  81.       return $offset + $pos;
  82.    }
  83.  
  84.  
  85.  
  86.    /**
  87.     * Make a string uppercase atleast for ISO-8859-1 (Latin 1) full with all
  88.     * special characters.
  89.     *
  90.     * @version 1.0
  91.     * @since   0.1.95
  92.     * @access  public
  93.     * @see     http://www.php.net/manual/en/function.strtoupper.php#78498
  94.     *
  95.     * @static
  96.     *
  97.     * @param  string $str 
  98.     *
  99.     * @return string 
  100.     */
  101.    function strToUpper( $str )
  102.    {
  103.        // convert to entities
  104.        $subject = htmlentities( $str, ENT_QUOTES );
  105.        $pattern  = '/&([a-z])(uml|acute|circ';
  106.        $pattern .= '|tilde|ring|elig|grave|slash|horn|cedil|th);/e';
  107.        $replace  = "'&'.strtoupper('\\1').'\\2'.';'";
  108.        $result  = preg_replace( $pattern, $replace, $subject );
  109.  
  110.        // convert from entities back to characters
  111.        $htmltable = get_html_translation_table( HTML_ENTITIES );
  112.  
  113.        foreach ( $htmltable as $key => $value )
  114.        {  $result = ereg_replace( addslashes( $value ), $key, $result ); }
  115.  
  116.        return strtoupper( $result );
  117.    }
  118.  
  119.  
  120.  
  121.    /**
  122.     * Replaces substrings in string by the matching keys of an array to the
  123.     * value of that key.
  124.     *
  125.     * Example:
  126.     * <code>
  127.     * $str = listReplace(
  128.     *                  array(
  129.     *                          "ü" => "ue",
  130.     *                          "aniel" => "."
  131.     *                       ),
  132.     *                  "Daniel Plücken"
  133.     *                    );
  134.     *
  135.     * echo $str;
  136.     * </code>
  137.     *
  138.     * Puts out "D. Pluecken".
  139.     *
  140.     * @version 1.0
  141.     * @since   0.1.9
  142.     * @access  public
  143.     *
  144.     * @static
  145.     *
  146.     * @param   array   $array  The array that holds the replace list.
  147.     * @param   string  $string The string which should be modified.
  148.     * @return  string 
  149.     */
  150.    function listReplace( $array, $string )
  151.    {
  152.       $str_out = $string;
  153.       foreach ( $array as $key => $value )
  154.          $str_out = str_replace( $key, $value, $str_out );
  155.  
  156.       return $str_out;
  157.    }
  158.  
  159.  
  160.  
  161.    /**
  162.     * Escapes all line breaks in a string with \.
  163.     *
  164.     * @version 1.0
  165.     * @since   0.1.8
  166.     * @access  public
  167.     *
  168.     * @static
  169.     *
  170.     * @param   string  $text The string which should be modified.
  171.     *
  172.     * @return  string 
  173.     */
  174.    function escapeLineBreaks( $text )
  175.    {
  176.       $text = str_replace(  "\r"  , "\\r", $text );
  177.       $text = str_replace(  "\n"  , "\\n", $text );
  178.  
  179.       return $text;
  180.    }
  181.  
  182.  
  183.  
  184.    /**
  185.     * Removes all line breaks in a string.
  186.     *
  187.     * @version 1.0
  188.     * @since   0.1.0
  189.     * @access  public
  190.     *
  191.     * @static
  192.     *
  193.     * @param   string  $text The string which should be modified.
  194.     *
  195.     * @return  string 
  196.     */
  197.    function removeLineBreaks( $text )
  198.    {
  199.       $text = str_replace(  "\r"  , "", $text );
  200.       $text = str_replace(  "\n"  , "", $text );
  201.  
  202.       return $text;
  203.    }
  204.  
  205.  
  206.  
  207.    /**
  208.     * Removes all white spaces including tabs and line breaks in a string.
  209.     *
  210.     * @version 1.0
  211.     * @since   0.1.0
  212.     * @access  public
  213.     *
  214.     * @static
  215.     *
  216.     * @param   string  $text The string which should be modified.
  217.     *
  218.     * @return  string 
  219.     */
  220.    function removeWhiteSpace( $text )
  221.    {
  222.       $text = str_replace(  "\t", "", $text );
  223.       $text = str_replace(  "\r", "", $text );
  224.       $text = str_replace(  "\n", "", $text );
  225.       $text = preg_replace( "! +!" , "", $text );
  226.  
  227.       return trim( $text );
  228.    }
  229.  
  230.  
  231.  
  232.    /**
  233.     * Reduces in series lying spaces to only one space. Also all spaces at the
  234.     * beginning of a line will be removed.
  235.     *
  236.     * @version 1.1
  237.     * @since   0.1.0
  238.     * @access  public
  239.     *
  240.     * @static
  241.     *
  242.     * @param   string  $text The string which should be modified.
  243.     *
  244.     * @return  string 
  245.     */
  246.    function reduceWhiteSpace( $text )
  247.    {
  248.       $text = preg_replace( "!  +!",  " ", $text );
  249.       return trim( $text );
  250.    }
  251.  
  252.  
  253.  
  254.    /**
  255.     * Adds $char to string until string has the length of $digits.
  256.     *
  257.     * Example:
  258.     * Strings::leadingChar( "0", "5", 4 );
  259.     * returns "0005"
  260.     *
  261.     * @version 1.1
  262.     * @since   0.1.0
  263.     * @access  public
  264.     *
  265.     * @static
  266.     *
  267.     * @param   char    $char   The char that should add to $string.
  268.     * @param   string  $string The string that schould be extended.
  269.     * @param   integer $digits The count of digits $string schould get.
  270.     *
  271.     * @return  string 
  272.     */
  273.    function leadingChar( $char, $string, $digits )
  274.    {
  275.       $tmp = $digits - strlen( $string );
  276.  
  277.       if( $tmp > 0 )
  278.         return str_repeat( $char, $tmp ).$string;
  279.       else
  280.         return $string;
  281.    }
  282.  
  283.  
  284.  
  285.    /**
  286.     * Returns whether the given string begins with substring $what.
  287.     *
  288.     * @version 1.1
  289.     * @since   0.1.0
  290.     * @access  public
  291.     *
  292.     * @static
  293.     *
  294.     * @param   string  $string The string which should be checked if it starts
  295.     *                           with $what
  296.     * @param   string  $what   The substring which will be compared with the
  297.     *                           beginning of $string
  298.     *
  299.     * @return  boolean 
  300.     */
  301.    function startsWith( $string, $what )
  302.    {
  303.       $what = preg_quote( $what );
  304.       return preg_match( "!^".$what."!i", $string );
  305.    }
  306.  
  307.  
  308.  
  309.    /**
  310.     * Returns whether the given string ends with substring $what.
  311.     *
  312.     * @version 1.0
  313.     * @since   0.1.0
  314.     * @access  public
  315.     *
  316.     * @static
  317.     *
  318.     * @param   string  $string The string which should be checked if it ends
  319.     *                           with $what
  320.     * @param   string  $what   The substring which will be compared with the
  321.     *                           end of $string
  322.     *
  323.     * @return  boolean 
  324.     */
  325.    function endsWith( $string, $what )
  326.    {
  327.       $what = preg_quote( $what );
  328.       return preg_match( "!".$what."$!i", $string );
  329.    }
  330.  
  331.  
  332.  
  333.    /**
  334.     * Splits a string by $separator. Every extracted piece will be concatenate
  335.     * with $beforeRPL at the beginning and $behindRPL at the ending of its own.
  336.     * At least all pieces will put together to a new string. Optionally the
  337.     * pieces will be separate by a new separator which is given with the fifth
  338.     * parameter.
  339.     *
  340.     * @version 1.1
  341.     * @since   0.1.1
  342.     * @access  public
  343.     *
  344.     * @static
  345.     *
  346.     * @param   string  $string 
  347.     * @param   string  $seperator 
  348.     * @param   string  $beforeRPL 
  349.     * @param   string  $behindRPL 
  350.     * @param   string  $seperatorRPL 
  351.     *
  352.     * @return  string 
  353.     */
  354.    function explodeReplace(
  355.                             $string,
  356.                             $separator,
  357.                             $beforeRPL,
  358.                             $behindRPL,
  359.                             $separatorRPL = ""
  360.                           )
  361.    {
  362.       $string = Strings::reduceWhiteSpace( $string );
  363.  
  364.       $tmp = explode( $separator, $string );
  365.       $out = $beforeRPL
  366.             .implode( $behindRPL.$separatorRPL.$beforeRPL, $tmp )
  367.             .$behindRPL;
  368.  
  369.       return $out;
  370.    }
  371.  
  372.  
  373.  
  374.    /**
  375.     * Returns the first Word from a text.
  376.     *
  377.     * @version 1.1
  378.     * @since   0.1.0
  379.     * @access  public
  380.     *
  381.     * @static
  382.     *
  383.     * @param   string  $string The string that schould be extended.
  384.     *
  385.     * @return  string 
  386.     */
  387.    function getFirstWord( $string )
  388.    {
  389.       $string = trim( $string );
  390.  
  391.       return preg_replace( "!^([^ \t\n\r]+).*$!is", "$1", $string );
  392.    }
  393.  
  394.  
  395.  
  396.    /**
  397.     * Returns a coincidence created password with given digits.
  398.     *
  399.     * @version 1.0
  400.     * @since   0.1.2
  401.     * @access  public
  402.     *
  403.     * @static
  404.     *
  405.     * @param   integer  $digits 
  406.     *
  407.     * @return  string 
  408.     */
  409.    function getRandomPassword( $digits = 8 )
  410.    {
  411.      if( !is_int( $digits ) )
  412.      { die( "\"".$digits."\" is not an integer!" ); }
  413.      else
  414.      {
  415.        $arr = array(
  416.                       'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
  417.                       'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  418.                       'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  419.                       'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
  420.                       'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3',
  421.                       '4', '5', '6', '7', '8', '9', '.', ',', ';', ':', '#',
  422.                       '+', '-', '!', '\"', '$', '\'', '*', '%', '/', '<', '>',
  423.                       '|', '(', ')', '[', ']', '{', '}', '?', '=', '&', '\\'
  424.                    );
  425.        $out = "";
  426.        for( $i = 0; $i < $digits; $i++ )
  427.          $out .= Arrays::getRandomRecord( $arr );
  428.  
  429.        return $out;
  430.      }
  431.  
  432.    }
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.    /**
  440.     * Returns a coincidence created word with given digits.
  441.     *
  442.     * @version 1.0
  443.     * @since   0.1.2
  444.     * @access  public
  445.     *
  446.     * @static
  447.     *
  448.     * @param   integer  $digits 
  449.     *
  450.     * @return  string 
  451.     */
  452.    function getRandomWord( $digits = 8 )
  453.    {
  454.      if( !is_int( $digits ) )
  455.      { die( "\"".$digits."\" is not an integer!" ); }
  456.      else
  457.      {
  458.        $arr = array(
  459.                       'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
  460.                       'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  461.                       'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  462.                       'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
  463.                       'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
  464.                    );
  465.        $out = "";
  466.        for( $i = 0; $i < $digits; $i++ )
  467.          $out .= Arrays::getRandomRecord( $arr );
  468.  
  469.        return $out;
  470.      }
  471.    }
  472.  
  473.  
  474.  
  475.    /**
  476.     * Interprets arguments given by an URL.
  477.     *
  478.     * @version 1.0
  479.     * @since   0.1.4
  480.     * @access  public
  481.     *
  482.     * @static
  483.     *
  484.     * @param   string  $string 
  485.     * @param   string  $kind 
  486.     *
  487.     * @return  void 
  488.     */
  489.    function parseParam( $string, $kind = "ALL" )
  490.    {
  491.      if( $string === "" )
  492.        return;
  493.  
  494.      if( !preg_match( "!GLOBALS|GET|POST|REQUEST|ALL!i", $kind ) )
  495.        die(
  496.              "Following values are possible to set the output-kind "
  497.             ."of the arguments:<br><br>"
  498.             ."GLOBALS (to store the values in the \"\x24GLOBALS\"-array)<br>"
  499.             ."GET (to store the values in the \"\x24_GET\"-array)<br>"
  500.             ."POST (to store the values in the \"\x24_POST\"-array)<br>"
  501.             ."REQUEST (to store the values in the \"\x24_REQUEST\"-array)<br><br>"
  502.             ."ALL (to store the values in every possible kind of array - "
  503.                  ."this overrules the other settings)<br><br>"
  504.             ."All kinds can be used in combination, but the \"ALL\"-kind "
  505.             ."overrules the other settings."
  506.           );
  507.  
  508.  
  509.      # extract tokens
  510.      $token = array();
  511.      preg_match_all( "![?&]([^?&]+)!", $string, $token, PREG_PATTERN_ORDER );
  512.  
  513.      for( $i = 0; $i < count( $token[1] ); $i++ )
  514.      {
  515.        # extract arguments and values
  516.        $param_val = array();
  517.        preg_match_all(
  518.                         "!^([^=]+)=(.*)$!",
  519.                         $token[1][$i], $param_val,
  520.                         PREG_PATTERN_ORDER
  521.                      );
  522.  
  523.        if( preg_match( "!REQUEST|ALL!i", $kind ) )
  524.          $_REQUEST[$param_val[1][0]] = $param_val[2][0];
  525.        if( preg_match( "!POST|ALL!i", $kind ) )
  526.          $_POST[$param_val[1][0]]    = $param_val[2][0];
  527.        if( preg_match( "!GET|ALL!i", $kind ) )
  528.          $_GET[$param_val[1][0]]     = $param_val[2][0];
  529.        if( preg_match( "!GLOBALS|ALL!i", $kind ) )
  530.          $GLOBALS[$param_val[1][0]]  = $param_val[2][0];
  531.      }
  532.    }
  533. } // End of class Strings
  534. ?>

Documentation generated on Thu, 05 Jun 2008 19:15:08 +0200 by phpDocumentor 1.4.1