|
|
|
| Title : |
STR - a Perl-like string manipulator class - The str class provides 4 perl-like methods for manipulating strings and
other scalar variables. |
| Categories : |
PHP, PHP Classes, Perl, Strings |
 Zak Greant |
| Date : |
Feb 14th 2001 |
| Grade : |
1 of 5 (graded 1 times) |
| Viewed : |
4931 |
| File : |
No file for this code example. |
| Images : |
No Images for this code example. |
|
| Search : |
More code by Zak Greant |
|
| Action : |
Grade This Code Example
|
|
| Tools : |
My Examples List |
|
|
|
|
|
|
<?php
/*
+----------------------------------------------------------------------+
| Copyright (c) 2000 J.A.Greant |
| (See end of file for usage notes and licensing information.) |
+----------------------------------------------------------------------+
*/
class str
{
function pop (&$string)
{
if ( 0 == strlen ($string) || is_array ($string))
return FALSE;
$pop = substr ($string, -1);
$string = substr ($string, 0, -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 ( 0 == strlen ($string) || is_array ($string))
return FALSE;
$unshift = substr ($string, 0, 1);
$string = substr ($string, 1);
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. |
+----------------------------------------------------------------------+
*/
?> |
|
| PHP5 URL Object Categories : PHP, PHP Classes, URLs, Strings | | | Class TStringList include some metods from class TStringList
implemented in INPRISE/BORLAND-DELPHI Categories : PHP Classes, PHP, Strings | | | Compare two texts and display a block of text with the differences between them. Categories : PHP, PHP Classes, Filesystem, Strings, Arrays | | | PHP Object Example of the Perl DBI with MySQL Categories : PHP, PHP Classes, MySQL, Databases, Perl | | | Filter - A simple class that lets you use multiple functions to create custom filters. Categories : PHP, PHP Classes, Strings | | | file class , uploade file , download file already uploaded on another website Categories : PHP, PHP Classes, Filesystem, Web Services | | | Authorize.net AIM Interface Class v1.0.0 Categories : PHP, PHP Classes, Ecommerce, Payment Gateways | | | A simple class with some HTML output functions that would come in handy for consistent page layout etc. Categories : PHP, PHP Classes, HTML and PHP, HTML, Navigation | | | crop and resize image class using gd library function Categories : PHP, PHP Classes, GD image library, Graphics | | | Excel class in PHP Categories : PHP, PHP Classes, Excel | | | News management class Categories : PHP, PHP Classes, Beginner Guides | | | The class to check load time of your script
VERY usefull for relatively slow applications, but not only.. Categories : PHP, PHP Classes, Debugging | | | Avoiding or Detecting high bit characters in a string. Useful when you want to create a valid RSS feed Categories : PHP, Strings, Unicode, Regexps, Rich Site Summary (RSS) | | | Expose - PHP template engine, supports server and client-sided caching,a plugin system, multiple languages, template script language is based on PHP itself. Categories : PHP, PHP Classes, Templates, Complete Programs | | | Simple Template Class/Example Categories : PHP, Templates, PHP Classes | |
|
|
|