Strictly for PHP 5.3 I started to encapsulate PHP's string functions into a String namespace. Now I'm not doing just plan renaming of some of the functions, I'm working on building in UTF-8 support directly into them, since I work using UTF-8.
While I am at it, it was a good opertunity to reorder some of the argument. I havn't currently finished it.
PHP Code:
<?php
namespace PHP::Common::String;
const UTF8_AWARE = 0x80000000;
#-------------------------------------------------------------------------------
const CHANGE_ALL_CASE = 1
, CHANGE_FIRST_CASE = 2
, CHANGE_WORDS_CASE = 4;
function toLower ( $s, $f = 0 )
{
if ( $f === 0 || $f & 1 ) { return ::strtolower( $s ); }
if ( $f & 2 ) { $s = ::lcfirst( $s ); }
if ( $f & 4 ) { $s = Arrays::_w( $s )->map( '::lcfirst' )->join( ' ' ); }
return $s;
}
function toUpper ( $s, $f = 0 )
{
if ( $f === 0 || $f & 1 ) { return ::strtoupper( $s ); }
if ( $f & 2 ) { $s = ::ucfirst( $s ); }
if ( $f & 4 ) { $s = ::ucwords( $s ); }
return $s;
}
#-------------------------------------------------------------------------------
const TRIM_BOTH = 1
, TRIM_LEFT = 2
, TRIM_RIGHT = 4
, TRIM_KEEP_DEFAULT_MASK = 8;
function trim ( $string, $mask = null, $flags = 1 )
{
if ( !empty( $mask ) ) {
settype( $mask, 'string' );
if ( $flags & 8 ) { $mask .= "\x00..\x20"; }
} else { $mask = "\x00..\x20"; }
if ( ( $flags & ~8 ) === 0 || $flags & 1 ) { return ::trim( $string, $mask ); }
if ( $flags & 2 ) { $string = ::ltrim( $string, $mask ); }
if ( $flags & 4 ) { $string = ::rtrim( $string, $mask ); }
return $string;
}
#-------------------------------------------------------------------------------
const REPLACE_IGNORE_CASE = 1;
function replace ( $s, $n, $r = '', &$c = null, $f = 0 )
{
if ( $f & 1 ) { return ::str_ireplace( $n, $r, $s, $c ); }
return ::str_replace( $n, $r, $s, $c );
}
Reording the arguments gives the advantage of making a simple object like this:
PHP Code:
<?php
namespace PHP::Common::String;
class Object
{
protected $s;
public function __call ( $method, $args )
{
if ( $method === 'toString' ) { return $this->s; }
$func = $this->getFunction( $method );
if ( $func !== false ) {
$args = array_unshift( $args, $this->s );
$t = call_user_func_array( $func, $args );
if ( !is_string( $t ) ) { return $t; }
$this->s = $t;
return $this;
}
}
protected function getFunction ( $method )
{
$method = __NAMESPACE__ . '::' . $method;
return ( function_exists( $method ) ? $method : false );
}
#---------------------------------------------------------------------------
public function __construct ( $s ) { $this->s = (string)$s; }
public function __toString () { return $this->s; }
#---------------------------------------------------------------------------
public function repeat () { throw new Exception( 'Not Implmented.' ); }
}
#-------------------------------------------------------------------------------
function _s ( $s ) { return new Object( $s ); }
Usuage example:
PHP Code:
<?php
use PHP::Common::String;
$s = String::_s( ' too much darn whitespace. ' )
->trim()
->replace( 'much', 'little' )
->replace( 'darn' )
->toUpper( String::CHANGE_WORDS_CASE );
print $s; # Too Little Whitespace.
// sp comment bug
Bookmarks