<?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>PHP Software Developing &#187; Strings</title>
	<atom:link href="http://www.phpdeveloping.co.za/category/strings/feed" rel="self" type="application/rss+xml" />
	<link>http://www.phpdeveloping.co.za</link>
	<description>for the love of PHP Development</description>
	<lastBuildDate>Tue, 29 Sep 2009 15:38:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Eval</title>
		<link>http://www.phpdeveloping.co.za/strings/using-eval.html</link>
		<comments>http://www.phpdeveloping.co.za/strings/using-eval.html#comments</comments>
		<pubDate>Tue, 11 Aug 2009 10:34:30 +0000</pubDate>
		<dc:creator>chris</dc:creator>
				<category><![CDATA[Strings]]></category>

		<guid isPermaLink="false">http://www.phpdeveloping.co.za/?p=123</guid>
		<description><![CDATA[I use this function very rarely.  For some reason I just never need to use it.  I know in Drupal it is used quite often, especially if you use the PHP input field for a node.  This allows you to enter PHP code that will be evaluated (or run) and then the [...]]]></description>
			<content:encoded><![CDATA[<p>I use this function very rarely.  For some reason I just never need to use it.  I know in Drupal it is used quite often, especially if you use the PHP input field for a node.  This allows you to enter PHP code that will be evaluated (or run) and then the output is shown as the node&#8217;s content.</p>
<p>I suppose this is mostly used when you want a user to be able to enter PHP code into a text field and then run it.  Not always too secure I would think, so be careful where and how you use this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  $s = &quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">phpinfo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;;
  eval($s);</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.phpdeveloping.co.za/strings/using-eval.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Does a string begin with another string</title>
		<link>http://www.phpdeveloping.co.za/strings/does-a-string-begin-with-another-string.html</link>
		<comments>http://www.phpdeveloping.co.za/strings/does-a-string-begin-with-another-string.html#comments</comments>
		<pubDate>Tue, 28 Jul 2009 16:05:15 +0000</pubDate>
		<dc:creator>chris</dc:creator>
				<category><![CDATA[Strings]]></category>
		<category><![CDATA[string starts with string]]></category>

		<guid isPermaLink="false">http://www.phpdeveloping.co.za/?p=75</guid>
		<description><![CDATA[Here is a function to check if a string begings with another string

  function startsWith&#40;$haystack,$needle,$case=true&#41; &#123;
    if&#40;$case&#41;  &#123; 
      return &#40;strcmp&#40;substr&#40;$haystack, 0, strlen&#40;$needle&#41;&#41;,$needle&#41;===0&#41;;
    &#125;
    return &#40;strcmp&#40;strtolower&#40;substr&#40;$haystack, 0, strlen&#40;$needle&#41;&#41;&#41;,strtolower&#40;$needle&#41;&#41;===0&#41;;
  &#125;

]]></description>
			<content:encoded><![CDATA[<p>Here is a function to check if a string begings with another string</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">function</span> startsWith<span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span><span style="color: #000088;">$needle</span><span style="color: #339933;">,</span><span style="color: #000088;">$case</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$case</span><span style="color: #009900;">&#41;</span>  <span style="color: #009900;">&#123;</span> 
      <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strcmp</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$needle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$needle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">===</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strcmp</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$needle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$needle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">===</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.phpdeveloping.co.za/strings/does-a-string-begin-with-another-string.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Does a string end with another string</title>
		<link>http://www.phpdeveloping.co.za/strings/does-a-string-end-with-another-string.html</link>
		<comments>http://www.phpdeveloping.co.za/strings/does-a-string-end-with-another-string.html#comments</comments>
		<pubDate>Tue, 28 Jul 2009 16:05:13 +0000</pubDate>
		<dc:creator>chris</dc:creator>
				<category><![CDATA[Strings]]></category>
		<category><![CDATA[string ends with string]]></category>

		<guid isPermaLink="false">http://www.phpdeveloping.co.za/?p=77</guid>
		<description><![CDATA[Here&#8217;s a nice function to check if a string ends with another string:

  function endsWith&#40;$haystack,$needle,$case=true&#41; &#123;
    if&#40;$case&#41;&#123;
      return &#40;strcmp&#40;substr&#40;$haystack, strlen&#40;$haystack&#41; - strlen&#40;$needle&#41;&#41;,$needle&#41;===0&#41;;
    &#125;
    return &#40;strcmp&#40;strtolower&#40;substr&#40;$haystack, strlen&#40;$haystack&#41; - strlen&#40;$needle&#41;&#41;&#41;,strtolower&#40;$needle&#41;&#41;===0&#41;;
  &#125;

]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a nice function to check if a string ends with another string:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">function</span> endsWith<span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span><span style="color: #000088;">$needle</span><span style="color: #339933;">,</span><span style="color: #000088;">$case</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$case</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strcmp</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$needle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$needle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">===</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strcmp</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$needle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$needle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">===</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.phpdeveloping.co.za/strings/does-a-string-end-with-another-string.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick and dirty template system</title>
		<link>http://www.phpdeveloping.co.za/strings/quick-and-dirty-template-system.html</link>
		<comments>http://www.phpdeveloping.co.za/strings/quick-and-dirty-template-system.html#comments</comments>
		<pubDate>Mon, 27 Jul 2009 07:41:12 +0000</pubDate>
		<dc:creator>chris</dc:creator>
				<category><![CDATA[Strings]]></category>
		<category><![CDATA[Templates]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[str_replace]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://www.phpdeveloping.co.za/?p=84</guid>
		<description><![CDATA[Before I knew about Smarty, I developed me own way of having HTML templates for the projects I developed.  Admittedly, I still use it even though Smarty is so much more powerful.  When I don&#8217;t need the versatility of Smarty, I just use my own method.
I started off by having a template directory [...]]]></description>
			<content:encoded><![CDATA[<p>Before I knew about <a href="http://www.smarty.net">Smarty</a>, I developed me own way of having HTML templates for the projects I developed.  Admittedly, I still use it even though Smarty is so much more powerful.  When I don&#8217;t need the versatility of Smarty, I just use my own method.</p>
<p>I started off by having a template directory with HTML files in it.  These files consist of all the HTML code I want, and has placeholders to where content will be.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;body&gt;
&lt;div&gt;%header%&lt;/div&gt;
&lt;div&gt;
  &lt;div&gt;%menu%&lt;/div&gt;
  &lt;div&gt;%content%&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;%footer%&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>As you can see, I have 4 place holders there, %header%, %menu%, %content% and %footer.</p>
<p>Now all I have to do is use str_replace to replace the place holders with content.</p>
<p>Here is an example of doing that</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;template.html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$menu</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;ul&gt;&lt;li&gt;Option 1&lt;/li&gt;&lt;li&gt;Option 2&lt;/li&gt;&lt;/ul&gt;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;h1&gt;This is the Header&lt;/h1&gt;&lt;br /&gt;%menu%&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;p&gt;This is some content.&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$footer</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;small&gt;This is the footer&lt;/small&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%header%&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$header</span><span style="color: #339933;">,</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%menu%&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$menu</span><span style="color: #339933;">,</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%c</span>ontent%&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$content</span><span style="color: #339933;">,</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%f</span>ooter%&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$footer</span><span style="color: #339933;">,</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$html</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you can see, I make use of the placeholder within another placeholder as well.  So the %menu% is also replace within the $header variable.  Just note if you want to use it like that, you need to order the str_replace so that it will get replaced.</p>
<p>The resulting HTML will be:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;body&gt;
&lt;div&gt;&lt;h1&gt;This is the Header&lt;/h1&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Option 1&lt;/li&gt;&lt;li&gt;Option 2&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;
&lt;div&gt;
  &lt;div&gt;&lt;ul&gt;&lt;li&gt;Option 1&lt;/li&gt;&lt;li&gt;Option 2&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;
  &lt;div&gt;&lt;p&gt;This is some content.&lt;/p&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;small&gt;This is the footer&lt;/small&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.phpdeveloping.co.za/strings/quick-and-dirty-template-system.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Strlen and Isset for string length</title>
		<link>http://www.phpdeveloping.co.za/strings/strlen-and-isset-for-string-length.html</link>
		<comments>http://www.phpdeveloping.co.za/strings/strlen-and-isset-for-string-length.html#comments</comments>
		<pubDate>Wed, 22 Jul 2009 16:07:35 +0000</pubDate>
		<dc:creator>chris</dc:creator>
				<category><![CDATA[Strings]]></category>

		<guid isPermaLink="false">http://www.phpdeveloping.co.za/?p=19</guid>
		<description><![CDATA[If you were asked to check up on the length of string, you&#8217;d probably use the following code:

$str = &#34;This is a string&#34;;
echo strlen&#40;$str&#41;;

Let&#8217;s look at a different way of doing this:

$str = 'This is a string';
if &#40;isset&#40;$str&#91;9&#93;&#41;&#41; &#123;
    echo 'The input is longer or equal then 10 characters.';
&#125; else  &#123;
 [...]]]></description>
			<content:encoded><![CDATA[<p>If you were asked to check up on the length of string, you&#8217;d probably use the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;This is a string&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Let&#8217;s look at a different way of doing this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This is a string'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The input is longer or equal then 10 characters.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span>  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The input is less then 10 characters long.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The equivalent with strlen:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This is a string'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The input is longer or equal then 10 characters.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The input is less then 10 characters long.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, why use isset instead of strlen?  I&#8217;ve read documents claiming that isset runs up to 5 time faster than strlen in most cases.  But that is not the main reason for wanting to use it.  Try using strlen on a variable that has not been initialized yet.  Yes, I know, that is what PHP and languages like it has done: allow people to use uninitialized variables.  And although I believe it to be bad programming practice to not initial variables, it happens.  Getting back to using strlen on an uninitialized variable.  You will get a &#8220;Notice&#8221; error telling you that it is not yet initialized.  Although not a major problem, it is an irritation seeing these notices if you have them enabled.</p>
<p>So, start using isset if you want a bit more performance or start remember to initialize variables and use strlen.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpdeveloping.co.za/strings/strlen-and-isset-for-string-length.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
