Well we know that in Javascript string can be handled as object in a way like this:
text = new String("This is a test string.");
This feature is unavailable in PHP, which is somewhat unfortunate seeing how PHP even adds object-oriented supports for Datetime. Nonetheless I've just come across a string class made by someone called Michael Scribellito, the script looks like it can be used by anyone. The code is:
<?php
/*
* String Class
*
* Allows Strings to be handled as objects.
*
* @author Michael Scribellito
* @link http://mscribellito.com
*/
final class String {
// holds the value of the String object
private $value;
public function __construct($value = '') {
$this->value = (string) $value;
}
public function charAt($index) {
if (abs($index) >= $this->length()) {
throw new Exception('Index out of bounds');
}
return substr($this->value, $index, 1);
}
public function charCodeAt($index) {
if (abs($index) >= $this->length()) {
throw new Exception('Index out of bounds');
}
return ord($this->charAt($index));
}
public function compareTo($that) {
if (!($that instanceof String)) {
$that = new String($that);
}
return strcmp($this->value, $that->value);
}
public function compareToIgnoreCase($that) {
if (!($that instanceof String)) {
$that = new String($that);
}
return strcmp($this->toLowerCase()->value, $that->toLowerCase()->value);
}
public function concat() {
$strs = func_get_args();
$temp = array();
foreach ($strs as $str) {
if (!($str instanceof String)) {
$str = new String($str);
}
$temp[] = $str->value;
}
return new String($this->value . implode('', $temp));
}
public function contains($sequence) {
return $this->indexOf($sequence) >= 0 ? TRUE : FALSE;
}
public function endsWith($suffix) {
return preg_match('/' . preg_quote($suffix) . '$/', $this->value);
}
public function equals($that) {
if (!($that instanceof String)) {
$that = new String($that);
}
return $this->value === $that->value;
}
public function equalsIgnoreCase($that) {
if (!($that instanceof String)) {
$that = new String($that);
}
return $this->toLowerCase()->value === $that->toLowerCase()->value;
}
public static function fromCharCode() {
$args = func_get_args();
$str = new String();
foreach ($args as $arg) {
$str = $str->concat(chr($arg));
}
return $str->value;
}
public function hashCode() {
$h = 0;
for ($i = 0, $l = $this->length(); $i < $l; $i++) {
$h = 31 * $h + ord($this->charAt($i));
}
return $h;
}
public function indexOf($substring, $fromIndex = 0) {
if ($fromIndex >= $this->length() || $fromIndex < 0) {
throw new Exception('Index out of bounds');
}
$index = strpos($this->value, $substring, $fromIndex);
return (is_int($index)) ? $index : -1;
}
public function isEmpty() {
return $this->length() === 0 ? TRUE : FALSE;
}
public function lastIndexOf($substring, $fromIndex = 0) {
if ($fromIndex >= $this->length() || $fromIndex < 0) {
throw new Exception('Index out of bounds');
}
$index = strrpos($this->value, $substring, $fromIndex);
return is_int($index) ? $index : -1;
}
public function length() {
return strlen($this->value);
}
public function matches($pattern) {
return preg_match($pattern, $this->value);
}
public function md5() {
return new String(md5($this->value));
}
public function quote() {
return new String('"' . $this->value . '"');
}
public function replace($old, $new, $count = NULL) {
if ($count !== NULL) {
$temp = str_replace($old, $new, $this->value, $count);
} else {
$temp = str_replace($old, $new, $this->value);
}
return new String($temp);
}
public function replaceAll($pattern, $replacement) {
$temp = preg_replace($pattern, $replacement, $this->value);
return new String($temp);
}
public function replaceFirst($pattern, $replacement) {
$temp = preg_replace($pattern, $replacement, $this->value, 1);
return new String($temp);
}
public function sha1() {
return new String(sha1($this->value));
}
public function split($pattern, $limit = NULL) {
return preg_split($pattern, $this->value, $limit);
}
/*
* Tests if this String starts with the specified prefix.
*
* @access public
* @param string
* @return boolean
*/
public function startsWith($prefix) {
return preg_match('/^' . preg_quote($prefix) . '/', $this->value);
}
/*
* Returns a new String that is a substring of this string.
*
* @access public
* @param int
* @param int
* @return String
*/
public function substring($start, $length = NULL) {
if ($length !== NULL) {
$temp = substr($this->value, $start, $length);
} else {
$temp = substr($this->value, $start);
}
return new String($temp);
}
/*
* Converts this String to an array of characters.
*
* @access public
* @return array
*/
public function toCharArray() {
$chars = array();
for ($i = 0, $l = $this->length(); $i < $l; $i++) {
$chars[] = $this->charAt($i);
}
return $chars;
}
/*
* Converts all of the characters in this String to lower case.
*
* @access public
* @return String
*/
public function toLowerCase() {
return new String(strtolower($this->value));
}
/*
* Converts all of the characters in this String to upper case.
*
* @access public
* @return String
*/
public function toUpperCase() {
return new String(strtoupper($this->value));
}
/*
* Removes leading and trailing whitespace.
*
* @access public
* @return String
*/
public function trim() {
$temp = preg_replace('/^\s+/', '', preg_replace('/\s+$/', '', $this->value));
return new String($temp);
}
/*
* Removes leading whitespace.
*
* @access public
* @return String
*/
public function trimLeft() {
$temp = preg_replace('/^\s+/', '', $this->value);
return new String($temp);
}
/*
* Removes trailing whitespace.
*
* @access public
* @return String
*/
public function trimRight() {
$temp = preg_replace('/\s+$/', '', $this->value);
return new String($temp);
}
/*
* Returns the value of this String
*
* @access public
* @return string
*/
public function __toString() {
return $this->value;
}
}
Perhaps there are still some new methods that can be added to this class, but it does seem to provide a way for strings to be handled like objects. The question is, would you recommend using PHP string in object-oriented way? Just curious.