Zend - The PHP Company




Text

Add Code


STR - a Perl-like string manipulator class  

Type: class
Added by: zak
Entered: 24/11/2000
Last modified: 01/12/2000
Rating: - (fewer than 3 votes)
Views: 6987
The str class provides 4 perl-like methods for manipulating strings and other scalar variables.

Methods include:
    pop() - Strip the rightmost character from a string and return the character.

    push() - Join one or more scalar values onto the right end of a scalar variable.
     The values are concatenated from left to right, in the order that they were specified.

    shift() - Strip the leftmost character from a string and return the character.

    unshift() - Join one or more scalar values onto the left end of a scalar variable.
     The values are concatenated from right to left, in the order that they were specified.


<?php
/*
    +----------------------------------------------------------------------+
    | Copyright (c) 2000 J.A.Greant                                        |
    | (See end of file for usage notes and licensing information.)         |
    +----------------------------------------------------------------------+
*/

class str
{
    function 
pop (&$string)
    {
        if ( 
== strlen ($string) || is_array ($string))
            return 
FALSE;

        
$pop substr ($string, -1);
        
$string substr ($string0, -1);
        return 
$pop;
    }

    function 
push (&$var)
    {
        if (
is_array ($var) || is_object ($var) || is_resource ($var))
            return 
FALSE;

        
$args func_get_args ();
        
$var implode (''$args);
        return 
strlen ($var);
    }

    function 
shift (&$string)
    {
        if ( 
== strlen ($string) || is_array ($string))
            return 
FALSE;

        
$unshift substr ($string01);
        
$string substr ($string1);
        return 
$unshift;
    }

    function 
unshift (&$var)
    {
        if (
is_array ($var) || is_object ($var) || is_resource ($var))
            return 
FALSE;

        
$args array_reverse (func_get_args ());
        
$var implode (''$args);
        return 
strlen ($var);
    }
}
/*
    +----------------------------------------------------------------------+
    | FILE NAME: str.php                                                   |
    +----------------------------------------------------------------------+
    | Author: J.A.Greant                                                   |
    | Email : zak@nucleus.com                                              |
    | Date  : 2000/11/23                                                   |
    +----------------------------------------------------------------------+
    | The str class provides 4 perl-like methods for manipulating strings  |
    | and other scalar variables.                                          |
    |                                                                      |
    |--> WARNING: The following methods operate directly on the values  <--|
    |--> passed to them. pop and shift shorten the value passed, while  <--|
    |--> push and unshift make the first variable passed to the method  <--|
    |--> contain all of the values passed to the method.                <--|
    |                                                                      |
    | str::pop() method                                                    |
    | -----------------                                                    |
    | Strip the rightmost character from a scalar variable and return the  |
    | character.                                                           |
    | NOTE: pop() must be used on a variable and not a literal value.      |
    |                                                                      |
    | USAGE EXAMPLE:                                                       |
    | $string = "Humpty Dumpty sat on a wall.";                            |
    | $chr = str::pop ($string);                                           |
    | print $chr; // Contains "."                                          |
    | print $string; // Contains "Humpty Dumpty sat on a wall"             |
    |                                                                      |
    | str::push() method                                                   |
    | ------------------                                                   |
    | Join one or more scalar values onto the right end of a scalar        |
    | variable. The values are concatenated from left to right, in the     |
    | order that they were specified.                                      |
    | NOTE: The first argument for push() must be a variable and not a     |
    | literal value. All other arguments may be literal values.            |
    |                                                                      |
    | USAGE EXAMPLE:                                                       |
    | $string = "Humpty Dumpty sat on a wall.n";                          |
    | $string_2 = "Humpty Dumpty had a great fall.n";                     |
    | $length = str::push ($string, $string_2);                            |
    | print $length; // Contains 61                                        |
    | print $string; // Contains $string . $string_2                       |
    |                                                                      |
    | str::shift() method                                                  |
    | -------------------                                                  |
    | Strip the leftmost character from a scalar variable and return the   |
    | character.                                                           |
    | NOTE: shift() must be used on a variable and not a literal value.    |
    |                                                                      |
    | USAGE EXAMPLE:                                                       |
    | $string = "Humpty Dumpty sat on a wall.";                            |
    | $chr = str::shift ($string);                                         |
    | print $chr; // Contains "H"                                          |
    | print $string; // Contains "umpty Dumpty sat on a wall."             |
    |                                                                      |
    | str::unshift() method                                                |
    | ------------------                                                   |
    | Join one or more scalar values onto the left end of a scalar         |
    | variable. The values are concatenated from right to left, in the     |
    | order that they were specified.                                      |
    | NOTE: The first argument for unshift() must be a variable and not a  |
    | literal value. All other arguments may be literal values.            |
    |                                                                      |
    | USAGE EXAMPLE:                                                       |
    | $string = 'fe';                                                      |
    | $length = str::unshift ($string, 'fi', 'fo', 'foo');                 |
    | print $length; // Contains 9                                         |
    | print $string; // Contains "foofofife"                               |
    +----------------------------------------------------------------------+

    +----------------------------------------------------------------------+
    | CVS LOG INFO                                                         |
    +----------------------------------------------------------------------+
      $Log: str.pkg,v $
      Revision 1.3  2001/01/02 03:56:14  zak
      Added tests to handle empty strings for pop and shift.
      
      $Log: str.pkg,v $
      Revision 1.2  2000/11/24 02:08:04  zak
      Simplified code for push and unshift.
      Removed extraneous comments.

      Revision 1.1.1.1  2000/11/24 01:49:50  zak
      Initial import of php code
      repository


    +----------------------------------------------------------------------+
    | Copyright (c) 2000 J.A.Greant (zak@nucleus.com)                      |
    | All rights reserved.                                                 |
    +----------------------------------------------------------------------+
    | Redistribution and use in source and binary forms, with or without   |
    | modification, is permitted provided that the following conditions    |
    | are met:                                                             |
    +----------------------------------------------------------------------+
    | Redistributions of source code must retain the above copyright       |
    | notice, this list of conditions and the following disclaimer.        |
    |                                                                      |
    | Redistributions in binary form must reproduce the above copyright    |
    | notice, this list of conditions and the following disclaimer in the  |
    | documentation and/or other materials provided with the distribution. |
    |                                                                      |
    | Neither the name of the author nor the names of any contributors to  |
    | this software may be used to endorse or promote products derived     |
    | from this software without specific prior written permission.        |
    |                                                                      |
    | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
    | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT  |
    | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
    | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE       |
    | AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,           |
    | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
    | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;     |
    | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER     |
    | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT   |
    | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN    |
    | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE      |
    | POSSIBILITY OF SUCH DAMAGE.                                          |
    +----------------------------------------------------------------------+
*/
?>


Usage Example


See documentation in source code


Rate This Script





Search



This Category All Categories