<?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>Case Asset Management AB &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/case-asset-management-ab/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>5 PHP String Functions You Need to Know</title>
		<link>/2010/09/17/5-php-string-functions-you-need-to-know/</link>
		<comments>/2010/09/17/5-php-string-functions-you-need-to-know/#comments</comments>
		<pubDate>Fri, 17 Sep 2010 11:19:43 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Case Asset Management AB]]></category>
		<category><![CDATA[Foo bar]]></category>
		<category><![CDATA[Foobar]]></category>
		<category><![CDATA[php string functions]]></category>
		<category><![CDATA[php tutorial]]></category>
		<category><![CDATA[php upper cased strings]]></category>
		<category><![CDATA[possible solution]]></category>
		<category><![CDATA[speaker]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[string functions]]></category>
		<category><![CDATA[United States]]></category>

		<guid isPermaLink="false">/?p=1984</guid>
		<description><![CDATA[The Task First of all what we&#8217;d like to achieve? The task is to convert a string, most of the cases single word, by capitalize the first letter. In my case I&#8217;ve the world countries names all lower cased, while I need them with first letter capitalized. In example &#8220;united states&#8221; must become &#8220;United States&#8221;, &#8230; <a href="/2010/09/17/5-php-string-functions-you-need-to-know/" class="more-link">Continue reading <span class="screen-reader-text">5 PHP String Functions You Need to Know</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2011/08/18/powerful-php-less-known-string-manipulation/" rel="bookmark" title="Powerful PHP: Less Known String Manipulation">Powerful PHP: Less Known String Manipulation </a></li>
<li><a href="/2012/03/16/you-think-you-know-php-quiz-results/" rel="bookmark" title="You think you know PHP. Quiz Results!">You think you know PHP. Quiz Results! </a></li>
<li><a href="/2009/10/11/strict-types-in-zend-framework-and-in-php/" rel="bookmark" title="Strict types in Zend Framework and in PHP">Strict types in Zend Framework and in PHP </a></li>
<li><a href="/2011/08/17/php-fetch-get-as-string-with-http_build_query/" rel="bookmark" title="PHP: Fetch $_GET as String with http_build_query()">PHP: Fetch $_GET as String with http_build_query() </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p><a href="/wp-content/uploads/2010/09/string.jpg"><img class="aligncenter size-full wp-image-1991" title="strings in PHP" src="/wp-content/uploads/2010/09/string.jpg" alt="Strings in PHP" width="430" height="184" srcset="/wp-content/uploads/2010/09/string.jpg 430w, /wp-content/uploads/2010/09/string-300x128.jpg 300w" sizes="(max-width: 430px) 100vw, 430px" /></a></p>
<h2>The Task</h2>
<p>First of all what we&#8217;d like to achieve? The task is to convert a string, most of the cases single word, by capitalize the first letter. In my case I&#8217;ve the world countries names all lower cased, while I need them with first letter capitalized. In example &#8220;united states&#8221; must become &#8220;United States&#8221;, but not &#8220;United states&#8221; or &#8220;UNITED STATES&#8221;. So here began the journey into PHP string functions, especially those for capitalization!</p>
<h2>1. ucwords</h2>
<p>The first thing you find in the <a title="PHP Manual" href="http://www.php.net/manual/en/index.php" target="_blank">PHP Manual</a> is the <a title="ucwords" href="http://php.net/manual/en/function.ucwords.php" target="_blank">ucwords</a> function. It changes the first letter to a capital letter, but it does not do the job. Why? Well let me show you an example.</p>
<pre lang="php">$str1 = 'foo bar';
$str2 = 'Foo bar';
$str3 = 'FOO BAR';
$str4 = 'фуу бар';

echo ucwords($str1); // Foo Bar
echo ucwords($str2); // Foo Bar
echo ucwords($str3); // FOO BAR
echo ucwords($str4); // фуу бар
</pre>
<p>Here we have four strings. A lower cased, an upper cased, a mixed cased and &#8230; a lower cased Cyrillic string. First of all the main reason why ucwords doesn&#8217;t fit here is because of the Cyrillic string. Whatever non-Latin string you have you can forget about capitalization. However the other strings conversions are also interesting. Take a look at the third string! Here the string remains &#8220;FOO BAR&#8221; instead of going &#8220;Foo Bar&#8221;, which simply means that this function only looks, and hopefully changes, the first letter.</p>
<p>So here we have two questions. How can we overcome the Cyrillic problem and how to &#8220;normalize&#8221; the UPPER CASE string?</p>
<h2>2. ucfirst</h2>
<p>This is another useful function in PHP. <a title="ucfirst" href="http://www.php.net/manual/en/function.ucfirst.php" target="_blank">ucfirst</a> as you may guess from its name converts a string by only changing its first letter. So &#8220;Foo bar&#8221; will remain &#8220;Foo bar&#8221;, while with ucwords it has become &#8220;Foo Bar&#8221;. Let&#8217;s see what this function does:</p>
<pre lang="php">$str1 = 'foo bar';
$str2 = 'Foo bar';
$str3 = 'FOO BAR';
$str4 = 'фуу бар';

echo ucfirst($str1); // Foo bar
echo ucfirst($str2); // Foo bar
echo ucfirst($str3); // FOO BAR
echo ucfirst($str4); // фуу бар
</pre>
<p>Here even the first string has only one capital letter &#8211; &#8220;foo bar&#8221; became &#8220;Foo bar&#8221;, and yet again we&#8217;ve the Cyrillic string unchanged. It simply doesn&#8217;t help us here!</p>
<h2>3. mb_convert_case</h2>
<p>As a PHP developer you know what the &#8220;mb_&#8221; prefix means &#8211; multibyte. This is quite useful. You can convert the string whatever the encoding is, so perhaps we can overcome the Cyrillic problem. But before proceeding to tests, let&#8217;s take a look at the parameters of this function.</p>
<p>The first thing to note here is that <a title="mb_convert_case" href="http://www.php.net/manual/en/function.mb-convert-case.php" target="_blank">mb_convert_case</a> doesn&#8217;t contain the case in its name &#8211; upper or lower. There&#8217;s a second parameter, after the first which is the string itself, who setups that. Note that here you don&#8217;t have the typical camel case or capitals parameter name, but MB_CASE_TITLE (as you know in English the title is always capitalized):</p>
<pre lang="php">echo mb_convert_case($str, MB_CASE_TITLE, ...
</pre>
<p>And a third one which specifies the encoding:</p>
<pre lang="php">echo mb_convert_case($str, MB_CASE_TITLE, 'utf-8')
</pre>
<p>Now let&#8217;s see what we can achieve with it:</p>
<pre lang="php">$str1 = 'foo bar';
$str2 = 'Foo bar';
$str3 = 'FOO BAR';
$str4 = 'фуу бар';

echo mb_convert_case($str1, MB_CASE_TITLE, 'utf-8'); // Foo Bar
echo mb_convert_case($str2, MB_CASE_TITLE, 'utf-8'); // Foo Bar
echo mb_convert_case($str3, MB_CASE_TITLE, 'utf-8'); // Foo Bar
echo mb_convert_case($str4, MB_CASE_TITLE, 'utf-8'); // Фуу Бар
</pre>
<p>As you can see now the Cyrillic problem doesn&#8217;t exists and mb_convert_case is intelligent enough to change &#8220;FOO BAR&#8221; into &#8220;Foo Bar&#8221; &#8211; as I said this is the English style titling. That is by no means the solution when you deal with capitalization with different encoding.</p>
<p>However there is another approach to overcome the all UPPER CASE conversion problem. A possible solution is to convert the string first to a lower case string.</p>
<h2>4. strtolower</h2>
<p><a title="strtolower" href="http://php.net/manual/en/function.strtolower.php" target="_blank">strtolower</a> is very useful PHP string function and perhaps any PHP developer has used it at least once. But yet again &#8211; it does not do the job. Again because of the encoding problem.</p>
<pre lang="php">echo strtolower('ФУУ БАР'); // #*&amp;$(#*%#
</pre>
<p>As you can see the Cyrillic string cannot be lower cased! Let&#8217;s search again into the &#8220;mb_&#8221; universe.</p>
<h2>5. mb_strtolower</h2>
<p>This is the function. Again you&#8217;ve to specify the encoding:</p>
<pre lang="php">
echo mb_strtolower('ФУУ БАР', 'utf-8')
</pre>
<h2>Conclusion</h2>
<p>It doesn&#8217;t matter whether you&#8217;re native English speaker or not. Most of the web sites are multilingual and you cannot be sure what happens when you convert strings in Alphabets different from the Latin. Thus be careful even when everything seems to be OK with Latin string tests.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2011/08/18/powerful-php-less-known-string-manipulation/" rel="bookmark" title="Powerful PHP: Less Known String Manipulation">Powerful PHP: Less Known String Manipulation </a></li>
<li><a href="/2012/03/16/you-think-you-know-php-quiz-results/" rel="bookmark" title="You think you know PHP. Quiz Results!">You think you know PHP. Quiz Results! </a></li>
<li><a href="/2009/10/11/strict-types-in-zend-framework-and-in-php/" rel="bookmark" title="Strict types in Zend Framework and in PHP">Strict types in Zend Framework and in PHP </a></li>
<li><a href="/2011/08/17/php-fetch-get-as-string-with-http_build_query/" rel="bookmark" title="PHP: Fetch $_GET as String with http_build_query()">PHP: Fetch $_GET as String with http_build_query() </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/09/17/5-php-string-functions-you-need-to-know/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
