{"id":49,"date":"2011-02-15T11:56:40","date_gmt":"2011-02-15T11:56:40","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=49"},"modified":"2011-02-15T12:21:53","modified_gmt":"2011-02-15T12:21:53","slug":"normalizing-a-string-example-php","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/normalizing-a-string-example-php\/","title":{"rendered":"Normalizing a string (PHP)"},"content":{"rendered":"<p>We&#39;ll present one algorithm below which demonstrates the use of a loop and the <a class=\"target-blank\" href=\"http:\/\/www.php.net\/manual\/en\/function.ord.php\">ord<\/a> function to step through the characters in a string.<\/p>\n<p>In our case, we wish to normalize strings which are formatted as such:<\/p>\n<p>unitedStates, tomSmith, fordFocus<\/p>\n<p>to return their human readable form (United States, Tom Smith, Ford Focus)<\/p>\n<p>If you are not familiar with regular expressions, the following function below serves as a good example:<\/p>\n<p><code>function normalizeString($tstring) {<br \/>\n\t&nbsp;&nbsp;&nbsp; <br \/>\n\t&nbsp;&nbsp;&nbsp; $normalizedString = &#39;&#39;;<br \/>\n\t&nbsp;&nbsp;&nbsp; $position = 0;<br \/>\n\t&nbsp;&nbsp;&nbsp; <br \/>\n\t&nbsp;&nbsp;&nbsp; while($char = substr($tstring, $position, 1)) {<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (ord($char) &gt;=65 &amp;&amp; ord($char) &lt;= 90)<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $normalizedString .= &#39; &#39;;<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $normalizedString .= $char;<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $position += 1;<br \/>\n\t&nbsp;&nbsp;&nbsp; }<br \/>\n\t&nbsp;&nbsp;&nbsp; <br \/>\n\t&nbsp;&nbsp;&nbsp; return ucfirst($normalizedString);<br \/>\n\t&nbsp;&nbsp;&nbsp; <br \/>\n\t}<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Now, to do the exact same with regular expressions:<\/p>\n<p><code>function normalizeString($string) {<br \/>\n\t&nbsp;&nbsp;&nbsp; <br \/>\n\t&nbsp;&nbsp;&nbsp; return ucfirst(<a class=\"target-blank\" href=\"http:\/\/php.net\/manual\/en\/function.preg-replace.php\">preg_replace<\/a>(&#39;\/([A-Z])\/&#39;, &#39; $1&#39;, $string));<br \/>\n\t&nbsp;&nbsp;&nbsp; <br \/>\n\t}<\/code><\/p>\n<p>In our tests, the regular expression approach ran, on average, 10-15 percent faster than the loop approach.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We&#39;ll present one algorithm below which demonstrates the use of a loop and the ord function to step through the characters in a string. In&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[15,16],"tags":[17,18],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/49"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=49"}],"version-history":[{"count":3,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/49\/revisions"}],"predecessor-version":[{"id":52,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/49\/revisions\/52"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}