When some people start out in a new language, they are in it for the long haul. Other people sometimes just need to touch on it without getting into the details. PHP is one of those languages where you can do things in a quick and dirty way without getting into the details. I have written a simple class for the kind of person that just wants to get a few things done quickly that is familiar with VBScripting. The class is mainly a wrapper class that allows you to perform VBScript style methods on strings in PHP. For instance, you can use VBScript::Left($string, $length) instead of using PHP’s substr($string, 0, $length). Enjoy!
<?php /*****************************************************************************\ Author: Jeff Gibeau WebSite: http://www.NorthAtlantaWebDesign.com Purpose: A string operation wrapper class that allows you to use VBScript style methods in place of PHP style methods. Recreates all methods from: http://www.w3schools.com/VBscript/vbscript_ref_functions.asp#string \*****************************************************************************/ class VBString { //Returns the position of the first occurrence of one string within another. //The search begins at the first character of the string. function InStr($haystack, $needle, $startPosition = 0) { return strpos($haystack, $needle, $startPosition); } //Returns the position of the first occurrence of one string within another. //The search begins at the last character of the string. function InStrRev($haystack, $needle, $startPosition = 0) { if(floatval(phpversion()) >= 5) return strrpos($haystack, $needle, $startPosition); else return strrpos($haystack, $needle); } //Converts a specified string to lowercase. function LCase($string) { return strtolower($string); } //Returns a specified number of characters from the left side of a string. function Left($string, $length) { return substr($string, 0, $length); } //Returns the number of characters in a string. function Len($string) { return strlen($string); } //Removes spaces on the left side of a string. function LTrim($string) { return ltrim($string); } //Removes spaces on the right side of a string. function RTrim($string) { return rtrim($string); } //Removes spaces on both the left and the right side of a string. function Trim($string) { return trim($string); } //Returns a specified number of characters from a string. function Mid($string, $start, $length = "") { if(!empty($length)) return substr($string, $start, $length); else return substr($string, $start); } //Replaces a specified part of a string with another string a specified //number of times. function Replace($haystack, $needle, $replaceWith, $start = -1, $count = -1) { $returnVal = $haystack; if($start != -1) { if($count != -1 && floatval(phpversion()) >= 5) { $returnVal = substr($haystack, 0, $start) . str_replace($needle, $replaceWith, substr($haystack, $start), $count); } else { $returnVal = substr($haystack, 0, $start) . str_replace($needle, $replaceWith, substr($haystack, $start)); } } else { if($count != -1 && floatval(phpversion()) >= 5) { $returnVal = str_replace($needle, $replaceWith, $haystack, $count); } else { $returnVal = str_replace($needle, $replaceWith, $haystack); } } return $returnVal; } //Returns a specified number of characters from the right side of a string. function Right($string, $count) { return substr($string, ($count * -1)); } //Returns a string that consists of a specified number of spaces. function Space($numSpaces) { $returnVal = ''; for($i = 0; $i < $numSpaces; $i++) $returnVal .= ' '; return $returnVal; } //Compares two strings and returns a value that represents the result of //the comparison. function StrComp($string1, $string2) { return strcmp($string1, $string2); } //Returns a string that contains a repeating character of a specified length. function String($length, $character) { $returnVal = ''; while(strlen($returnVal) < $length) $returnVal .= $character; return substr($returnVal, 0, $length); } //Reverses a string. function StrReverse($string) { return strrev($string); } //Converts a specified string to uppercase. function UCase($string) { return strtoupper($string); } } ?>
Using this class
<?php require_once 'vbstring_class.php'; $string1 = ' The quick brown fox jumped over the lazy dog. '; $string2 = 'he'; $string3 = 'hat'; $nl = "\n"; echo ' <style> * { font-family:courier; } span.answer { color:red; background:#eeeeee;border:1px solid #ccc; } </style> <pre> '; //VBString::InStr echo 'VBString::InStr("'.$string1.'", "'.$string2.'") -> <span class="answer">'. VBString::InStr($string1, $string2).'</span>'.$nl; //VBString::InStrRev echo 'VBString::InStrRev("'.$string1.'", "'.$string2.'") -> <span class="answer">'. VBString::InStrRev($string1, $string2).'</span>'.$nl; //VBString::LCase echo 'VBString::LCase("'.$string1.'") -> <span class="answer">"'. VBString::LCase($string1).'"</span>'.$nl; //VBString::Left echo 'VBString::Left("'.$string1.'", 5) -> <span class="answer">"'.VBString::Left($string1, 5).'"</span>'.$nl; //VBString::Len echo 'VBString::Len("'.$string1.'") -> <span class="answer">'.VBString::Len($string1).'</span>'.$nl; //VBString::LTrim echo 'VBString::LTrim("'.$string1.'") -> <span class="answer">"'.VBString::LTrim($string1).'"</span>'.$nl; //VBString::RTrim echo 'VBString::RTrim("'.$string1.'") -> <span class="answer">"'.VBString::RTrim($string1).'"</span>'.$nl; //VBString::Trim echo 'VBString::Trim("'.$string1.'") -> <span class="answer">"'.VBString::Trim($string1).'"</span>'.$nl; //VBString::Mid echo 'VBString::Mid("'.$string1.'", 5, 5) -> <span class="answer">"'.VBString::Mid($string1, 5, 5).'"</span>'.$nl; //VBString::Replace echo 'VBString::Replace("'.$string1.'", "'.$string2.'", "'.$string3.'", 5) -> <span class="answer">"'.VBString::Replace($string1, $string2, $string3, 5).'"</span>'.$nl; //VBString::Right echo 'VBString::Right("'.$string1.'", 5) -> <span class="answer">"'.VBString::Right($string1, 5).'"</span>'.$nl; //VBString::Space echo 'VBString::Space(10) -> <span class="answer">"'.VBString::Space(10).'"</span>'.$nl; //VBString::StrComp echo 'VBString::StrComp("'.$string1.'", "'.$string1.'") -> <span class="answer">"'.VBString::StrComp($string1, $string1).'"</span>'.$nl; //VBString::String echo 'VBString::String(10, "*") -> <span class="answer">"'.VBString::String(10, "*").'"</span>'.$nl; //VBString::StrReverse echo 'VBString::StrReverse("'.$string1.'") -> <span class="answer">"'.VBString::StrReverse($string1).'"</span>'.$nl; //VBString::UCase echo 'VBString::UCase("'.$string1.'") -> <span class="answer">"'. VBString::UCase($string1).'"</span>'.$nl; echo '</pre>'; ?>
Useful Links: