{"id":172,"date":"2009-08-24T16:42:52","date_gmt":"2009-08-24T20:42:52","guid":{"rendered":"http:\/\/www.northatlantawebdesign.com\/?p=172"},"modified":"2009-08-24T16:42:52","modified_gmt":"2009-08-24T20:42:52","slug":"vbscript-string-methods-in-a-simple-php-class","status":"publish","type":"post","link":"https:\/\/www.northatlantawebdesign.com\/index.php\/2009\/08\/24\/vbscript-string-methods-in-a-simple-php-class\/","title":{"rendered":"VBScript String Methods in a Simple PHP Class"},"content":{"rendered":"<p>When some people start out in a new language, they are in it for the long haul.\u00a0 Other people sometimes just need to touch on it without getting into the details.\u00a0 <a type=\"amzn\">PHP<\/a> is one of those languages where you can do things in a quick and dirty way without getting into the details.\u00a0 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.\u00a0 The class is mainly a wrapper class that allows you to perform <a type=\"amzn\">VBScript<\/a> style methods on strings in PHP.\u00a0 For instance, you can use VBScript::Left($string, $length) instead of using PHP&#8217;s substr($string, 0, $length).\u00a0 Enjoy!<br \/>\n<!--more--><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\/*****************************************************************************\\\r\nAuthor: Jeff Gibeau\r\nWebSite: http:\/\/www.NorthAtlantaWebDesign.com\r\nPurpose:\r\nA string operation wrapper class that allows you to use VBScript style methods\r\nin place of PHP style methods.  Recreates all methods from:\r\nhttp:\/\/www.w3schools.com\/VBscript\/vbscript_ref_functions.asp#string\r\n\\*****************************************************************************\/\r\nclass VBString\r\n{\r\n\t\/\/Returns the position of the first occurrence of one string within another.\r\n\t\/\/The search begins at the first character of the string.\r\n\tfunction InStr($haystack, $needle, $startPosition = 0)\r\n\t{\r\n\t\treturn strpos($haystack, $needle, $startPosition);\r\n\t}\r\n\t\r\n\t\/\/Returns the position of the first occurrence of one string within another.\r\n\t\/\/The search begins at the last character of the string.\r\n\tfunction InStrRev($haystack, $needle, $startPosition = 0)\r\n\t{\r\n\t\tif(floatval(phpversion()) &gt;= 5)\r\n\t\t\treturn strrpos($haystack, $needle, $startPosition);\r\n\t\telse\r\n\t\t\treturn strrpos($haystack, $needle);\r\n\t}\r\n\t\r\n\t\/\/Converts a specified string to lowercase.\r\n\tfunction LCase($string)\r\n\t{\r\n\t\treturn strtolower($string);\r\n\t}\r\n\t\r\n\t\/\/Returns a specified number of characters from the left side of a string.\r\n\tfunction Left($string, $length)\r\n\t{\r\n\t\treturn substr($string, 0, $length);\r\n\t}\r\n\t\r\n\t\/\/Returns the number of characters in a string.\r\n\tfunction Len($string)\r\n\t{\r\n\t\treturn strlen($string);\r\n\t}\r\n\t\r\n\t\/\/Removes spaces on the left side of a string.\r\n\tfunction LTrim($string)\r\n\t{\r\n\t\treturn ltrim($string);\r\n\t}\r\n\t\r\n\t\/\/Removes spaces on the right side of a string.\r\n\tfunction RTrim($string)\r\n\t{\r\n\t\treturn rtrim($string);\r\n\t}\r\n\t\r\n\t\/\/Removes spaces on both the left and the right side of a string.\r\n\tfunction Trim($string)\r\n\t{\r\n\t\treturn trim($string);\r\n\t}\r\n\t\r\n\t\/\/Returns a specified number of characters from a string.\r\n\tfunction Mid($string, $start, $length = &quot;&quot;)\r\n\t{\r\n\t\tif(!empty($length))\r\n\t\t\treturn substr($string, $start, $length);\r\n\t\telse\r\n\t\t\treturn substr($string, $start);\r\n\t}\r\n\t\r\n\t\/\/Replaces a specified part of a string with another string a specified\r\n\t\/\/number of times.\r\n\tfunction Replace($haystack, $needle, $replaceWith, $start = -1, $count = -1)\r\n\t{\r\n\t\t$returnVal = $haystack;\r\n\t\tif($start != -1)\r\n\t\t{\r\n\t\t\tif($count != -1 &amp;&amp; floatval(phpversion()) &gt;= 5)\r\n\t\t\t{\r\n\t\t\t\t$returnVal = substr($haystack, 0, $start) . str_replace($needle, $replaceWith, substr($haystack, $start), $count);\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\t$returnVal = substr($haystack, 0, $start) . str_replace($needle, $replaceWith, substr($haystack, $start));\r\n\t\t\t}\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tif($count != -1 &amp;&amp; floatval(phpversion()) &gt;= 5)\r\n\t\t\t{\r\n\t\t\t\t$returnVal = str_replace($needle, $replaceWith, $haystack, $count);\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\t$returnVal = str_replace($needle, $replaceWith, $haystack);\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn $returnVal;\r\n\t}\r\n\t\r\n\t\/\/Returns a specified number of characters from the right side of a string.\r\n\tfunction Right($string, $count)\r\n\t{\r\n\t\treturn substr($string, ($count * -1));\r\n\t}\r\n\t\r\n\t\/\/Returns a string that consists of a specified number of spaces.\r\n\tfunction Space($numSpaces)\r\n\t{\r\n\t\t$returnVal = '';\r\n\t\tfor($i = 0; $i &lt; $numSpaces; $i++)\r\n\t\t\t$returnVal .= ' ';\r\n\t\treturn $returnVal;\r\n\t}\r\n\t\r\n\t\/\/Compares two strings and returns a value that represents the result of\r\n\t\/\/the comparison.\r\n\tfunction StrComp($string1, $string2)\r\n\t{\r\n\t\treturn strcmp($string1, $string2);\r\n\t}\r\n\t\r\n\t\/\/Returns a string that contains a repeating character of a specified length.\r\n\tfunction String($length, $character)\r\n\t{\r\n\t\t$returnVal = '';\r\n\t\twhile(strlen($returnVal) &lt; $length)\r\n\t\t\t$returnVal .= $character;\r\n\t\treturn substr($returnVal, 0, $length);\r\n\t}\r\n\t\r\n\t\/\/Reverses a string.\r\n\tfunction StrReverse($string)\r\n\t{\r\n\t\treturn strrev($string);\r\n\t}\r\n\t\r\n\t\/\/Converts a specified string to uppercase.\r\n\tfunction UCase($string)\r\n\t{\r\n\t\treturn strtoupper($string);\r\n\t}\r\n}\r\n?&gt;\r\n<\/pre>\n<p>Using this class<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\nrequire_once 'vbstring_class.php';\r\n$string1 = '  The quick brown fox jumped over the lazy dog.  ';\r\n$string2 = 'he';\r\n$string3 = 'hat';\r\n$nl = &quot;\\n&quot;;\r\necho '\r\n&lt;style&gt;\r\n* { font-family:courier; }\r\nspan.answer { color:red; background:#eeeeee;border:1px solid #ccc; }\r\n&lt;\/style&gt;\r\n&lt;pre&gt;\r\n';\r\n\/\/VBString::InStr\r\necho 'VBString::InStr(&quot;'.$string1.'&quot;, &quot;'.$string2.'&quot;) -&gt; &lt;span class=&quot;answer&quot;&gt;'. VBString::InStr($string1, $string2).'&lt;\/span&gt;'.$nl;\r\n\r\n\/\/VBString::InStrRev\r\necho 'VBString::InStrRev(&quot;'.$string1.'&quot;, &quot;'.$string2.'&quot;) -&gt; &lt;span class=&quot;answer&quot;&gt;'. VBString::InStrRev($string1, $string2).'&lt;\/span&gt;'.$nl;\r\n\r\n\/\/VBString::LCase\r\necho 'VBString::LCase(&quot;'.$string1.'&quot;) -&gt; &lt;span class=&quot;answer&quot;&gt;&quot;'. VBString::LCase($string1).'&quot;&lt;\/span&gt;'.$nl;\r\n\r\n\/\/VBString::Left\r\necho 'VBString::Left(&quot;'.$string1.'&quot;, 5) -&gt; &lt;span class=&quot;answer&quot;&gt;&quot;'.VBString::Left($string1, 5).'&quot;&lt;\/span&gt;'.$nl;\r\n\r\n\/\/VBString::Len\r\necho 'VBString::Len(&quot;'.$string1.'&quot;) -&gt; &lt;span class=&quot;answer&quot;&gt;'.VBString::Len($string1).'&lt;\/span&gt;'.$nl;\r\n\r\n\/\/VBString::LTrim\r\necho 'VBString::LTrim(&quot;'.$string1.'&quot;) -&gt; &lt;span class=&quot;answer&quot;&gt;&quot;'.VBString::LTrim($string1).'&quot;&lt;\/span&gt;'.$nl;\r\n\r\n\/\/VBString::RTrim\r\necho 'VBString::RTrim(&quot;'.$string1.'&quot;) -&gt; &lt;span class=&quot;answer&quot;&gt;&quot;'.VBString::RTrim($string1).'&quot;&lt;\/span&gt;'.$nl;\r\n\r\n\/\/VBString::Trim\r\necho 'VBString::Trim(&quot;'.$string1.'&quot;) -&gt; &lt;span class=&quot;answer&quot;&gt;&quot;'.VBString::Trim($string1).'&quot;&lt;\/span&gt;'.$nl;\r\n\r\n\/\/VBString::Mid\r\necho 'VBString::Mid(&quot;'.$string1.'&quot;, 5, 5) -&gt; &lt;span class=&quot;answer&quot;&gt;&quot;'.VBString::Mid($string1, 5, 5).'&quot;&lt;\/span&gt;'.$nl;\r\n\r\n\/\/VBString::Replace\r\necho 'VBString::Replace(&quot;'.$string1.'&quot;, &quot;'.$string2.'&quot;, &quot;'.$string3.'&quot;, 5) -&gt; &lt;span class=&quot;answer&quot;&gt;&quot;'.VBString::Replace($string1, $string2, $string3, 5).'&quot;&lt;\/span&gt;'.$nl;\r\n\r\n\/\/VBString::Right\r\necho 'VBString::Right(&quot;'.$string1.'&quot;, 5) -&gt; &lt;span class=&quot;answer&quot;&gt;&quot;'.VBString::Right($string1, 5).'&quot;&lt;\/span&gt;'.$nl;\r\n\r\n\/\/VBString::Space\r\necho 'VBString::Space(10) -&gt; &lt;span class=&quot;answer&quot;&gt;&quot;'.VBString::Space(10).'&quot;&lt;\/span&gt;'.$nl;\r\n\r\n\/\/VBString::StrComp\r\necho 'VBString::StrComp(&quot;'.$string1.'&quot;, &quot;'.$string1.'&quot;) -&gt; &lt;span class=&quot;answer&quot;&gt;&quot;'.VBString::StrComp($string1, $string1).'&quot;&lt;\/span&gt;'.$nl;\r\n\r\n\/\/VBString::String\r\necho 'VBString::String(10, &quot;*&quot;) -&gt; &lt;span class=&quot;answer&quot;&gt;&quot;'.VBString::String(10, &quot;*&quot;).'&quot;&lt;\/span&gt;'.$nl;\r\n\r\n\/\/VBString::StrReverse\r\necho 'VBString::StrReverse(&quot;'.$string1.'&quot;) -&gt; &lt;span class=&quot;answer&quot;&gt;&quot;'.VBString::StrReverse($string1).'&quot;&lt;\/span&gt;'.$nl;\r\n\r\n\/\/VBString::UCase\r\necho 'VBString::UCase(&quot;'.$string1.'&quot;) -&gt; &lt;span class=&quot;answer&quot;&gt;&quot;'. VBString::UCase($string1).'&quot;&lt;\/span&gt;'.$nl;\r\n\r\necho '&lt;\/pre&gt;';\r\n?&gt;\r\n<\/pre>\n<p>Useful Links:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.w3schools.com\/VBscript\/vbscript_ref_functions.asp#string\">W3Schools VBScript String Methods<\/a><\/li>\n<li><a href=\"http:\/\/us2.php.net\/manual\/en\/book.strings.php\">PHP Strings Manual<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>When some people start out in a new language, they are in it for the long haul.\u00a0 Other people sometimes just need to touch on it without getting into the details.\u00a0 PHP is one of those languages where you can do things in a quick and dirty way without getting into the details.\u00a0 I have [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,23],"tags":[26,79,44,75,45],"class_list":["post-172","post","type-post","status-publish","format-standard","hentry","category-php","category-vbscript","tag-class","tag-php","tag-strings","tag-vbscript","tag-wrapper"],"_links":{"self":[{"href":"https:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/posts\/172","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/comments?post=172"}],"version-history":[{"count":5,"href":"https:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/posts\/172\/revisions"}],"predecessor-version":[{"id":177,"href":"https:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/posts\/172\/revisions\/177"}],"wp:attachment":[{"href":"https:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/media?parent=172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/categories?post=172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/tags?post=172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}