<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Learn PHP Online &#187; PHP Functions</title>
	<atom:link href="http://www.learnphponline.com/category/functions/feed" rel="self" type="application/rss+xml" />
	<link>http://www.learnphponline.com</link>
	<description>Learn PHP Online</description>
	<lastBuildDate>Wed, 06 May 2009 01:02:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Making Uppercase Words Automatically In PHP With UCWords</title>
		<link>http://www.learnphponline.com/functions/php-ucwords-making-uppercase-words-automatically</link>
		<comments>http://www.learnphponline.com/functions/php-ucwords-making-uppercase-words-automatically#comments</comments>
		<pubDate>Fri, 03 Apr 2009 23:28:19 +0000</pubDate>
		<dc:creator>Zachary Schuessler</dc:creator>
				<category><![CDATA[PHP Functions]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php capitalization]]></category>
		<category><![CDATA[php strings]]></category>
		<category><![CDATA[php tutorials]]></category>
		<category><![CDATA[php ucwords]]></category>
		<category><![CDATA[php uppercase]]></category>

		<guid isPermaLink="false">http://www.learnphponline.com/?p=49</guid>
		<description><![CDATA[Using UCWords to capitalize strings in PHP.]]></description>
			<content:encoded><![CDATA[<p style="border-top: 1px solid blue; border-bottom: 1px solid blue; margin: 0px auto; padding: 10px; width: 95%; background-color: #e0eaef; text-indent: 0px;"><strong>Foreword</strong>: Humans are lazy by nature; we hold this truth to be self-evident. Any function in PHP that is going to save us a bit of time down the road is well worth the time invested in as a result.</p>
<p>Ever since the release of PHP 4, we&#8217;ve had the ability to utilize a function that automatically converts a string to uppercase characters. This function, UCWords, has many uses that aren&#8217;t so obvious at first glance.</p>
<ol>
<li><strong>Title Case</strong> &#8211; While not everyone runs their own news website or even a movie database, we use title case for things such as website titles and website links. You&#8217;ll note in your browser window that your very own LearnPHPOnline.com utilizes the UCWords function to capitalize the title string.</li>
<li><strong>Sanitizing Database Input</strong> &#8211; We are all familiar with the stereotypical Internet user that just has to leave the Caps Lock key on at all times to get their point across. Protection against LOUD TEXT is quite prevalent in forum systems. (Try putting in &#8220;TEST TITLE&#8221; in vBulletin and watch it magically get transformed into &#8220;Test Title&#8221; if the forum is running correct settings.)</li>
<li><strong>Meta Tags</strong> &#8211; While you don&#8217;t see it directly, search engines still look at meta tags to help formulate what a website is and what it has to offer. By setting the &#8220;Title&#8221; attribute to automatically create uppercase words, we can show the search engines how proper we are without risking carpal tunnel syndrome.</li>
<li><strong>Fixing User Input</strong> &#8211; Let&#8217;s say you run your own business, and you contact registered users via E-mail to gain leads. If the user signed up and didn&#8217;t capitalize their name properly, it&#8217;s going to look unprofessional sending an email to them with their name in lowercase. In this situation we could actually lose business due to a lack of proper programming.</li>
</ol>
<h3>What Does The PHP Function UCWords Do?</h3>
<p>The UCWords function automatically capitalizes every word within a given string. The function is especially easy to use since it only accepts a single argument: the string you want to be forced into uppercase stance. We can see an example of the usage of ucwords below, in our news headline demonstration.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">PHP UCWords Example Of Basic Title Casing</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #0000bb;">&lt;?PHP

 $newsHeadline </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"breaking: man finds way to capitalize news headings automatically"</span><span style="color: #007700;">;

 echo </span><span style="color: #0000bb;">ucwords</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$newsHeadline</span><span style="color: #007700;">);</span>

<span style="color: #ff8000;"> //Returns: Breaking: Man Finds Way To Capitalize News Headings Automatically</span>

<span style="color: #0000bb;">?&gt;</span></pre>
<p>So far things look fairly simple. But what exactly constitutes a new word for the UCWords function? Technically defined, a new word is going to be a string of characters following a whitespace, so long as the string doesn&#8217;t begin with a number. What do you think would happen if we were to start a string with an integer? Would the next letter be capitalized? In the below example, we take the real-life Internet company 43Things.com into consideration.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">PHP UCWords Example Of Strings Starting With An Integer</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #0000bb;">&lt;?PHP

 $webpageTitle </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"43things.com"</span><span style="color: #007700;">;
 echo </span><span style="color: #0000bb;">ucwords</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$webpageTitle</span><span style="color: #007700;">);</span>
<span style="color: #ff8000;"> //Returns 43things.com</span>

<span style="color: #0000bb;">?&gt;</span></pre>
<p>Note that in this example, the T will not be forced into capitalization because the string begins with a number. If we were to separate the number and the letter with a space, carriage return, newline, tab, or form-feed, then the letter would indeed be capitalized.</p>
<p>If the UCWords function encounters a string that is already capitalized, the function will not act on the string in question. If, however, the user were to convert the entire string to lower case letters and then pass the result to the UCWords function, we get a result as seen in the below example.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">PHP UCWords Example Of Basic Rules Of Operation</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #0000bb;">&lt;?PHP

 $screamingText </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"THIS IS VERY LOUD TEXT"

 </span><span style="color: #007700;">echo </span><span style="color: #0000bb;">ucwords</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$screamingText</span><span style="color: #007700;">);

</span><span style="color: #ff8000;">//Returns: THIS IS VERY LOUD TEXT

 </span><span style="color: #007700;">echo </span><span style="color: #0000bb;">ucwords</span><span style="color: #007700;">(</span><span style="color: #0000bb;">strtolower</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$screamingText</span><span style="color: #007700;">));
</span><span style="color: #ff8000;">//Returns: This Is Very Loud Text

</span><span style="color: #0000bb;">?&gt;</span></pre>
<h3>Capitalizing Only Certain Words In A String With UCWords</h3>
<p>If we were concerned with only capitalizing a certain part of a string, we could write up a quick function to ensure that words that are commonly not capitalized are ignored. In many headings you may notice prepositions such as &#8220;at&#8221; or &#8220;to&#8221; aren&#8217;t capitalized. The following function solves the problem by accepting only a single argument.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">PHP UCWords Function For Returning Partially Capitalized String</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #0000bb;">&lt;?PHP

    </span><span style="color: #007700;">function </span><span style="color: #0000bb;">smartcase</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$str</span><span style="color: #007700;">) {
       return </span><span style="color: #0000bb;">preg_replace</span><span style="color: #007700;">(
            </span><span style="color: #dd0000;">"/(?&lt;=(?&lt;!:|'s)\W)(A|An|And|At|For|In|Of|On|Or|The|To|With)(?=\W)/e"</span><span style="color: #007700;">,

            </span><span style="color: #dd0000;">'strtolower("$1")'</span><span style="color: #007700;">,
            </span><span style="color: #0000bb;">$str</span><span style="color: #007700;">);

    }

echo </span><span style="color: #0000bb;">smartcase</span><span style="color: #007700;">(</span><span style="color: #0000bb;">ucwords</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"the prepositions in a title can be excluded with this function!"</span><span style="color: #007700;">));</span>

<span style="color: #ff8000;">//Output: The Prepositions in a Title Can Be Excluded with This Function!</span>
<span style="color: #0000bb;">?&gt;</span></pre>
<p>We could add other words to the ignore feature simply by editing the following line:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">PHP UCWords Example Of Basic Title Casing</p>
<pre style="border: 1px solid black; padding: 10px;">     (A|An|And|At|For|In|Of|On|Or|The|To|With)</pre>
<h3>Sanitize Database Input With A Function Using UCWords</h3>
<p>One of the more practical uses for UCWords is to sanitize input when putting information into a database. You may have noticed that some forum systems such as vBulletin will automatically change your thread title into title case if it is in all-caps. (After all, who really likes to read a screaming title?) An example of how this is done can be seen below, by implementing another function.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">PHP UCWords Example Of Sanitizing Input</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #0000bb;">&lt;?PHP</span>

<span style="color: #007700;">function </span><span style="color: #0000bb;">titleCase</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$string</span><span style="color: #007700;">)
     {
     return </span><span style="color: #0000bb;">ucwords</span><span style="color: #007700;">(</span><span style="color: #0000bb;">strtolower</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$string</span><span style="color: #007700;">));

     }</span>

<span style="color: #0000bb;">$threadName </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"CHECK OUT THIS NEW PHP FUNCTION GUYS!"</span><span style="color: #007700;">;
echo </span><span style="color: #0000bb;">titleCase</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$threadName</span><span style="color: #007700;">);</span>

<span style="color: #ff8000;">//Returns: Check Out This New PHP Function Guys!</span>

<span style="color: #0000bb;">?&gt;</span></pre>
<h4>Closing Comments</h4>
<p>UCWords has quite a bit of use to the PHP development community. Other functions that are worthy of noting would be: StrToUpper() , StrToLower() , UCFirst() , and MB_Convert_Case(). Each of these functions are necessary as the Internet shifts to a more and more dynamic environment.</p>
<p style="text-indent: 0px;"><strong>Bottom line</strong>: We can all thank technology for allowing us to reduce the wear and tear on our Shift keys.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learnphponline.com/functions/php-ucwords-making-uppercase-words-automatically/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
