<?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>Vdiscussion &#187; PHP</title>
	<atom:link href="http://vdiscussion.com/category/theory-subjects/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://vdiscussion.com</link>
	<description>Vinoth Kumar&#039;s Discussion</description>
	<lastBuildDate>Wed, 09 Dec 2009 15:34:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Finding the Current Date and Time in PHP Code</title>
		<link>http://vdiscussion.com/finding-the-current-date-and-time-in-php-code/</link>
		<comments>http://vdiscussion.com/finding-the-current-date-and-time-in-php-code/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 15:33:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Programs]]></category>
		<category><![CDATA[Theory Subjects]]></category>
		<category><![CDATA[Current Date and Time in PHP]]></category>
		<category><![CDATA[date()]]></category>
		<category><![CDATA[strftime()]]></category>

		<guid isPermaLink="false">http://vdiscussion.com/?p=150</guid>
		<description><![CDATA[Use strftime( ) or date( ) for a formatted time string Finding the current date and time &#60;?php print strftime(&#8216;%c&#8217;); print &#8220;\n&#8221;; print date(&#8216;r&#8217;); ?&#62; Output Wed May 10 18:29:59 2006 Wed, 10 May 2006 18:29:59 -0400 Finding the month, day, and year &#60;?php $a = getdate(); printf(&#8216;%s %d, %d&#8217;,$a['month'],$a['mday'],$a['year']); ?&#62;]]></description>
			<content:encoded><![CDATA[<p>Use <em>strftime( )</em> or <em>date( )</em> for a formatted time string</p>
<p><strong>Finding the current date and time</strong><br />
&lt;?php<br />
print strftime(&#8216;%c&#8217;);<br />
print &#8220;\n&#8221;;<br />
print date(&#8216;r&#8217;);<br />
?&gt;<br />
Output<br />
Wed May 10 18:29:59 2006<br />
Wed, 10 May 2006 18:29:59 -0400</p>
<p><strong>Finding the month, day, and year<br />
</strong>&lt;?php<br />
$a = getdate();<br />
printf(&#8216;%s %d, %d&#8217;,$a['month'],$a['mday'],$a['year']);<br />
?&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://vdiscussion.com/finding-the-current-date-and-time-in-php-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Calculate Exponents in PHP</title>
		<link>http://vdiscussion.com/how-to-calculate-exponents-in-php/</link>
		<comments>http://vdiscussion.com/how-to-calculate-exponents-in-php/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 15:12:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Programs]]></category>
		<category><![CDATA[Theory Subjects]]></category>
		<category><![CDATA[Calculate Exponents]]></category>
		<category><![CDATA[exp()]]></category>
		<category><![CDATA[pow()]]></category>

		<guid isPermaLink="false">http://vdiscussion.com/?p=147</guid>
		<description><![CDATA[To raise e to a power, use exp( ): $exp = exp(2);// 7.3890560989307 To raise it to any power, use pow( ): $exp = pow( 2, M_E);// 6.5808859910179 $pow = pow( 2, 10); // 1024 $pow = pow( 2, -2); // 0.25 $pow = pow( 2, 2.5);// 5.6568542494924 $pow = pow(-2, 10); // 1024 $pow [...]]]></description>
			<content:encoded><![CDATA[<p>To raise e to a power, use <em>exp( )</em>:<br />
$exp = exp(2);// 7.3890560989307</p>
<p>To raise it to any power, use <em>pow( )</em>:<br />
$exp = pow( 2, M_E);// 6.5808859910179</p>
<p>$pow = pow( 2, 10); // 1024<br />
$pow = pow( 2, -2); // 0.25<br />
$pow = pow( 2, 2.5);// 5.6568542494924</p>
<p>$pow = pow(-2, 10); // 1024<br />
$pow = pow( 2, -2); // 0.25<br />
$pow = pow(-2, -2.5); // NAN (Error: Not a Number)<br />
The built-in constant <tt>M_E</tt> is an approximation of the value of e. It equals 2.7182818284590452354. So <tt>exp($n)</tt> and <tt>pow(M_E, $n)</tt> are identical.<br />
It&#8217;s easy to create very large numbers using <em>exp( )</em> and <em>pow( )</em>; if you outgrow PHP&#8217;s maximum size (almost 1.8e308), how to use the arbitrary precision functions. With <em>exp( )</em> and <em>pow( )</em>, PHP returns <tt>INF</tt> (infinity) if the result is too large and <tt>NAN</tt> (not a number) on an error.</p>
]]></content:encoded>
			<wfw:commentRss>http://vdiscussion.com/how-to-calculate-exponents-in-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to use For Loop in PHP</title>
		<link>http://vdiscussion.com/how-to-use-for-loop-in-php/</link>
		<comments>http://vdiscussion.com/how-to-use-for-loop-in-php/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 15:09:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Programs]]></category>
		<category><![CDATA[Theory Subjects]]></category>
		<category><![CDATA[Loop in PHP]]></category>
		<category><![CDATA[range()]]></category>

		<guid isPermaLink="false">http://vdiscussion.com/?p=144</guid>
		<description><![CDATA[Use a for loop: &#60;?php for ($i = $start; $i &#60;= $end; $i++) { plot_point($i); } ?&#62; You can increment using values other than 1. For example: &#60;?php for ($i = $start; $i &#60;= $end; $i += $increment) { plot_point($i); } ?&#62; If you want to preserve the numbers for use beyond iteration, use the [...]]]></description>
			<content:encoded><![CDATA[<pre>Use a for loop:
&lt;?php
for ($i = $start; $i &lt;= $end; $i++) {
plot_point($i);
}
?&gt;
You can increment using values other than 1. For example:
&lt;?php
for ($i = $start; $i &lt;= $end; $i += $increment) {
plot_point($i);
}
?&gt;
If you want to preserve the numbers for use beyond iteration,
use the <em>range( )</em> method:
&lt;?php
$range = range($start, $end);
?&gt;
Loops like this are common. For instance, you could be plotting
a function and need to calculate the results for multiple points
on the graph. Or you could be a student counting down the number
of seconds until the end of school.
The <tt>for</tt> loop method uses a single integer and you have great
control over the loop, because you can increment and decrement
<tt>$i</tt> freely. Also, you can modify <tt>$i</tt> from inside the loop.
In the last example in the Solution, <em>range( )</em> returns an array
with values from <tt>$start</tt> to <tt>$end</tt>. The advantage of using <em>range( )
</em>is its brevity, but this technique has a few disadvantages. For
one, a large array can take up unnecessary memory. Also, you're
forced to increment the series one number at a time, so you can't
loop through a series of even integers, for example.
It's valid for <tt>$start</tt> to be larger than <tt>$end</tt>. In this case,
the numbers returned by <em>range( )</em> are in descending order. Also,
you can use it to retrieve character sequences:
&lt;?php
print_r(range('l', 'p'));
?&gt;
Array
(
[0] =&gt; l
[1] =&gt; m
[2] =&gt; n
[3] =&gt; o
[4] =&gt; p
)</pre>
]]></content:encoded>
			<wfw:commentRss>http://vdiscussion.com/how-to-use-for-loop-in-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Syntax to Rounding Floating-Point Numbers in PHP</title>
		<link>http://vdiscussion.com/syntax-to-rounding-floating-point-numbers-in-php/</link>
		<comments>http://vdiscussion.com/syntax-to-rounding-floating-point-numbers-in-php/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 15:05:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Programs]]></category>
		<category><![CDATA[Theory Subjects]]></category>
		<category><![CDATA[ceil()]]></category>
		<category><![CDATA[floor()]]></category>
		<category><![CDATA[round()]]></category>
		<category><![CDATA[Rounding Floating-Point Numbers]]></category>

		<guid isPermaLink="false">http://vdiscussion.com/?p=142</guid>
		<description><![CDATA[How to Rounding Floating-Point Numbers in PHP]]></description>
			<content:encoded><![CDATA[<p>To round a number to the closest integer, use <em>round( )</em> :<br />
$number = round(2.4); // $number = 2</p>
<p>To round up, use <em>ceil( ) </em>:<br />
$number = ceil(2.4);// $number = 3</p>
<p>To round down, use <em>floor( ) </em>:<br />
$number = floor(2.4); // $number = 2<br />
If a number falls exactly between two integers, PHP rounds away from 0:<br />
$number = round(2.5); //3</p>
<p>$number = round(-2.5);// -3<br />
To keep a set number of digits after the decimal point, <em>round( )</em> accepts an optional precision argument. For example, perhaps you are calculating the total price for the items in a user&#8217;s shopping cart:<br />
&lt;?php<br />
$cart = 54.23;<br />
$tax = $cart * .05;<br />
$total = $cart + $tax; // $total = 56.9415</p>
<p>$final = round($total, 2); // $final = 56.94<br />
?&gt;</p>
<p>To round a number down, use the <em>floor( )</em> function:<br />
$number = floor( 2.1);//2<br />
$number = floor( 2.9);//2<br />
$number = floor(-2.1);// -3<br />
$number = floor(-2.9);// -3</p>
<p>While to round up, use the <em>ceil( )</em> function:<br />
$number = ceil( 2.1);//3<br />
$number = ceil( 2.9);//3<br />
$number = ceil(-2.1);// -2<br />
$number = ceil(-2.9);// -2</p>
<p>These two functions are named because when you&#8217;re rounding down, you&#8217;re rounding &#8220;toward the floor,&#8221; and when you&#8217;re rounding up, you&#8217;re rounding &#8220;toward the ceiling.&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://vdiscussion.com/syntax-to-rounding-floating-point-numbers-in-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Variable contain valid number using PHP</title>
		<link>http://vdiscussion.com/variable-contain-valid-number-using-php/</link>
		<comments>http://vdiscussion.com/variable-contain-valid-number-using-php/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 16:40:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Programs]]></category>
		<category><![CDATA[Theory Subjects]]></category>
		<category><![CDATA[is_double( )]]></category>
		<category><![CDATA[is_float( )]]></category>
		<category><![CDATA[is_int( )]]></category>
		<category><![CDATA[is_integer( )]]></category>
		<category><![CDATA[is_long( )]]></category>
		<category><![CDATA[is_numeric( )]]></category>
		<category><![CDATA[is_real( )]]></category>
		<category><![CDATA[str_replace( )]]></category>

		<guid isPermaLink="false">http://vdiscussion.com/?p=140</guid>
		<description><![CDATA[You want to ensure that a variable contains a number, even if it's typed as a string. Alternatively, you want to check if a variable is not only a number, but is also specifically typed as a one.]]></description>
			<content:encoded><![CDATA[<p>How to check whether variable contains valid number using PHP</p>
<p>You want to ensure that a variable contains a number, even if it&#8217;s typed as a string. Alternatively, you want to check if a variable is not only a number, but is also specifically typed as a one.</p>
<p>Use is_numeric( ) to discover whether a variable contains a number:</p>
<p>&lt;?php<br />
if (is_numeric(5))          { /* true  */ }<br />
if (is_numeric(&#8217;5&#8242;))        { /* true  */ }<br />
if (is_numeric(&#8220;05&#8243;))       { /* true  */ }<br />
if (is_numeric(&#8216;five&#8217;))     { /* false */ }</p>
<p>if (is_numeric(0xDECAFBAD)) { /* true  */ }<br />
if (is_numeric(&#8220;10e200&#8243;))   { /* true  */ }<br />
?&gt;</p>
<p>Numbers come in all shapes and sizes. You cannot assume that something is a number simply because it only contains the characters 0 through 9. What about decimal points, or negative signs? You can&#8217;t simply add them into the mix because the negative must come at the front, and you can only have one decimal point. And then there&#8217;s hexadecimal numbers and scientific notation.</p>
<p>Instead of rolling your own function, use is_numeric( ) to check whether a variable holds something that&#8217;s either an actual number (as in it&#8217;s typed as an integer or floating point), or it&#8217;s a string containing characters that can be translated into a number.</p>
<p>There&#8217;s an actual difference here. Technically, the integer 5 and the string 5 aren&#8217;t the same in PHP. However, most of the time you won&#8217;t actually be concerned about the distinction, which is why the behavior of is_numeric( ) is useful.</p>
<p>Helpfully, is_numeric( ) properly parses decimal numbers, such as 5.1; however, numbers with thousands separators, such as 5,100, cause is_numeric( ) to return false.</p>
<p>To strip the thousands separators from your number before calling is_numeric( ), use str_replace( ):</p>
<p>&lt;?php<br />
is_numeric(str_replace($number, &#8216;,&#8217;, &#8221;));<br />
?&gt;</p>
<p>To check if your number is a specific type, there are a variety of related functions with self-explanatory names: is_float( ) (or is_double( ) or is_real( ); they&#8217;re all the same) and is_int( ) (or is_integer( ) or is_long( )).</p>
<p>To validate input data, use the techniques from Recipe 9.3 instead of is_numeric( ). That recipe describes how to check for positive or negative integers, decimal numbers, and a handful of other formats.</p>
]]></content:encoded>
			<wfw:commentRss>http://vdiscussion.com/variable-contain-valid-number-using-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Store Binary Data in Strings using PHP</title>
		<link>http://vdiscussion.com/how-to-store-binary-data-in-strings-using-php/</link>
		<comments>http://vdiscussion.com/how-to-store-binary-data-in-strings-using-php/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 16:34:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Programs]]></category>
		<category><![CDATA[Theory Subjects]]></category>
		<category><![CDATA[ASCII]]></category>
		<category><![CDATA[pack( )]]></category>
		<category><![CDATA[Store Binary Data in Strings]]></category>
		<category><![CDATA[unpack( )]]></category>

		<guid isPermaLink="false">http://vdiscussion.com/?p=136</guid>
		<description><![CDATA[How to Store Binary Data in Strings using PHP]]></description>
			<content:encoded><![CDATA[<p>You want to  parse a string that contains values encoded as a binary structure or encode  values into a string. For example, you want to store numbers in their binary  representation instead of as sequences of ASCII characters.</p>
<h4 id="title-IDAYIYPI">1.16.2. Solution</h4>
<p>Use <em>pack( )</em> to store binary data  in a string:</p>
<pre>&lt;?php
$packed = pack('S4',1974,106,28225,32725);
?&gt;</pre>
<p>Use <em>unpack( )</em><a name="ch01_unpackbin"></a> to extract  binary data from a string:</p>
<pre>&lt;?php
$nums = unpack('S4',$packed);
?&gt;</pre>
<p>1.16.3. Discussion</p>
<p>The first argument to <em>pack( )</em> is a format string that  describes how to encode the data that&#8217;s passed in the rest of the arguments. The  format string <tt>S4</tt> tells <em>pack( )</em> to produce four unsigned short  16-bit numbers in machine byte order from its input data. Given 1974, 106,  28225, and 32725 as input on a little-endian machine, this returns eight bytes:  182, 7, 106, 0, 65, 110, 213, and 127. Each two-byte pair corresponds to one of  the input numbers: 7 * 256 + 182 is 1974; 0 * 256 + 106 is 106; 110 * 256 + 65 =  28225; 127 * 256 + 213 = 32725.</p>
<p>The first argument to <em>unpack( )</em> is also a format string,  and the second argument is the data to decode. Passing a format string of  <tt>S4</tt>, the eight-byte sequence that <em>pack( )</em> produced returns a  four-element array of the original numbers. <tt>print_r($nums)</tt> prints:</p>
<pre>Array
(
    [1] =&gt; 1974
    [2] =&gt; 106
    [3] =&gt; 28225
    [4] =&gt; 32725
)</pre>
<p>In <em>unpack( )</em>, format characters and their count can be  followed by a string to be used as an array key. For example:</p>
<pre>&lt;?php
$nums = unpack('S4num',$packed);
print_r($nums);
?&gt;</pre>
<p>This prints:</p>
<pre>Array
(
    [num1] =&gt; 1974
    [num2] =&gt; 106
    [num3] =&gt; 28225
    [num4] =&gt; 32725
)</pre>
<p>Multiple format characters must be separated with <tt>/</tt> in  <em>unpack( )</em>:</p>
<pre>&lt;?php
$nums = unpack('S1a/S1b/S1c/S1d',$packed);
print_r($nums);
?&gt;</pre>
<p>This prints:</p>
<pre>Array
(
    [a] =&gt; 1974
    [b] =&gt; 106
    [c] =&gt; 28225
    [d] =&gt; 32725
)</pre>
]]></content:encoded>
			<wfw:commentRss>http://vdiscussion.com/how-to-store-binary-data-in-strings-using-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Wrap the Text in PHP</title>
		<link>http://vdiscussion.com/how-to-wrap-the-text-in-php/</link>
		<comments>http://vdiscussion.com/how-to-wrap-the-text-in-php/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 18:31:45 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Programs]]></category>
		<category><![CDATA[Theory Subjects]]></category>
		<category><![CDATA[wordwrap( )]]></category>
		<category><![CDATA[Wrap the Text]]></category>

		<guid isPermaLink="false">http://vdiscussion.com/?p=133</guid>
		<description><![CDATA[You need to wrap lines in a string. For example, you want to display text in &#60;pre&#62;/&#60;/pre&#62; tags but have it stay within a regularly sized browser window. Use wordwrap( ) : &#60;?php $s = &#8220;Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty and [...]]]></description>
			<content:encoded><![CDATA[<p>You need to wrap lines in a string. For example, you want to display text in <tt>&lt;pre&gt;</tt>/<tt>&lt;/pre&gt;</tt> tags but have it stay within a regularly sized browser window.<br />
Use <em>wordwrap( )</em> :</p>
<blockquote><p>&lt;?php<br />
$s = &#8220;Four score and seven years ago our fathers brought forth</p>
<p>on this continent a new nation, conceived in liberty and</p>
<p>dedicated to the proposition that all men are created equal.&#8221;;</p>
<p>print &#8220;&lt;pre&gt;\n&#8221;.wordwrap($s).&#8221;\n&lt;/pre&gt;&#8221;;<br />
?&gt;</p></blockquote>
<p>Output</p>
<blockquote><p>&lt;pre&gt;<br />
Four score and seven years ago our fathers brought forth on this continent<br />
a new nation, conceived in liberty and dedicated to the proposition that<br />
all men are created equal.<br />
&lt;/pre&gt;</p></blockquote>
<p>By default, <em>wordwrap( )</em> wraps text at 75 characters per line. An optional second argument specifies different line length:</p>
<blockquote><p>&lt;?php<br />
print wordwrap($s,50);<br />
?&gt;</p></blockquote>
<p><strong>Output</strong></p>
<blockquote><p>Four score and seven years ago our fathers brought<br />
forth on this continent a new nation, conceived in<br />
liberty and dedicated to the proposition that all<br />
men are created equal.</p></blockquote>
<p>Other characters besides \n can be used for line breaks. For double spacing, use &#8220;\n\n&#8221;:</p>
<blockquote><p>&lt;?php<br />
print wordwrap($s,50,&#8221;\n\n&#8221;);<br />
?&gt;</p></blockquote>
<p><strong>Output</strong></p>
<blockquote><p>Four score and seven years ago our fathers brought</p>
<p>forth on this continent a new nation, conceived in</p>
<p>liberty and dedicated to the proposition that all</p>
<p>men are created equal.</p></blockquote>
<p>There is an optional fourth argument to <em>wordwrap( )</em> that controls the treatment of words that are longer than the specified line length. If this argument is 1, these words are wrapped. Otherwise, they span past the specified line length:</p>
<blockquote><p>&lt;?php<br />
print wordwrap(&#8216;jabberwocky&#8217;,5);<br />
print wordwrap(&#8216;jabberwocky&#8217;,5,&#8221;\n&#8221;,1);<br />
?&gt;<br />
Output<br />
jabberwocky<br />
jabbe<br />
rwock<br />
y</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://vdiscussion.com/how-to-wrap-the-text-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to break a string into pieces in PHP</title>
		<link>http://vdiscussion.com/how-to-break-a-string-into-pieces-in-php/</link>
		<comments>http://vdiscussion.com/how-to-break-a-string-into-pieces-in-php/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 18:16:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Programs]]></category>
		<category><![CDATA[break a string into pieces]]></category>
		<category><![CDATA[explode( )]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Perl-compatible]]></category>
		<category><![CDATA[POSIX]]></category>
		<category><![CDATA[preg_split( )]]></category>
		<category><![CDATA[split( )]]></category>

		<guid isPermaLink="false">http://vdiscussion.com/?p=130</guid>
		<description><![CDATA[You need to break a string into pieces. For example, you want to access each line that a user enters in a &#60;textarea&#62; form field. Use explode( ) if what separates the pieces is a constant string: &#60;?php $words = explode(&#8216; &#8216;,&#8217;My sentence is not very complicated&#8217;); ?&#62; Use split( ) or preg_split( ) if [...]]]></description>
			<content:encoded><![CDATA[<p>You need to break a string into pieces. For example, you want to access each line that a user enters in a <tt>&lt;textarea&gt;</tt> form field.<br />
Use <em>explode( )</em> if what separates the pieces is a constant string:</p>
<blockquote><p>
&lt;?php<br />
$words = explode(&#8216; &#8216;,&#8217;My sentence is not very complicated&#8217;);<br />
?&gt;</p></blockquote>
<p>Use <em>split( ) </em>or <em>preg_split( )</em> if you need a POSIX or Perl-compatible regular expression to describe the separator:</p>
<blockquote><p>
&lt;?php<br />
$words = split(&#8216; +&#8217;,'This sentence  has  some extra whitespace  in it.&#8217;);<br />
$words = preg_split(&#8216;/\d\. /&#8217;,'my day: 1. get up 2. get dressed 3. eat toast&#8217;);<br />
$lines = preg_split(&#8216;/[\n\r]+/&#8217;,$_REQUEST['textarea']);<br />
?&gt;</p></blockquote>
<p>Use <em>spliti( )</em> or the /i flag to <em>preg_split( )</em> for case-insensitive separator matching:</p>
<blockquote><p>
&lt;?php<br />
$words = spliti(&#8216; x &#8216;,&#8217;31 inches x 22 inches X 9 inches&#8217;);<br />
$words = preg_split(&#8216;/ x /i&#8217;,&#8217;31 inches x 22 inches X 9 inches&#8217;);<br />
?&gt;</p></blockquote>
<p>The simplest solution of the bunch is <em>explode( )</em>. Pass it your separator string, the string to be separated, and an optional limit on how many elements should be returned:</p>
<blockquote><p>&lt;?php<br />
$dwarves = &#8216;dopey,sleepy,happy,grumpy,sneezy,bashful,doc&#8217;;<br />
$dwarf_array = explode(&#8216;,&#8217;,$dwarves);<br />
?&gt;</p></blockquote>
<p>This makes $dwarf_array a seven-element array, so print_r($dwarf_array) prints:<br />
Array<br />
(<br />
[0] =&gt; dopey<br />
[1] =&gt; sleepy<br />
[2] =&gt; happy<br />
[3] =&gt; grumpy<br />
[4] =&gt; sneezy<br />
[5] =&gt; bashful<br />
[6] =&gt; doc<br />
)</p>
<p>If the specified limit is less than the number of possible chunks, the last chunk contains the remainder:</p>
<blockquote><p>&lt;?php<br />
$dwarf_array = explode(&#8216;,&#8217;,$dwarves,5);<br />
print_r($dwarf_array);<br />
?&gt;</p></blockquote>
<p>This prints:<br />
Array<br />
(<br />
[0] =&gt; dopey<br />
[1] =&gt; sleepy<br />
[2] =&gt; happy<br />
[3] =&gt; grumpy<br />
[4] =&gt; sneezy,bashful,doc<br />
)</p>
<p>The separator is treated literally by <em>explode( )</em>. If you specify a comma and a space as a separator, it breaks the string only on a comma followed by a space, not on a comma or a space.<br />
With <em>split( )</em>, you have more flexibility. Instead of a string literal as a separator, it uses a POSIX regular expression:</p>
<blockquote><p>&lt;?php<br />
$more_dwarves = &#8216;cheeky,fatso, wonder boy, chunky,growly, groggy, winky&#8217;;<br />
$more_dwarf_array = split(&#8216;, ?&#8217;,$more_dwarves);<br />
?&gt;</p></blockquote>
<p>This regular expression splits on a comma followed by an optional space, which treats all the new dwarves properly. A dwarf with a space in his name isn&#8217;t broken up, but everyone is broken apart whether they are separated by &#8220;,&#8221; or &#8220;, &#8220;. print_r($more_dwarf_array) prints:<br />
Array<br />
(<br />
[0] =&gt; cheeky<br />
[1] =&gt; fatso<br />
[2] =&gt; wonder boy<br />
[3] =&gt; chunky<br />
[4] =&gt; growly<br />
[5] =&gt; groggy<br />
[6] =&gt; winky<br />
)</p>
<p>Similar to <em>split( )</em> is <em>preg_split( )</em>, which uses a Perl-compatible regular expression engine instead of a POSIX regular expression engine. With <em>preg_split( )</em>, you can take advantage of various Perl-ish regular expression extensions, as well as tricks such as including the separator text in the returned array of strings:</p>
<blockquote><p>&lt;?php<br />
$math = &#8220;3 + 2 / 7 &#8211; 9&#8243;;<br />
$stack = preg_split(&#8216;/ *([+\-\/*]) */&#8217;,$math,-1,PREG_SPLIT_DELIM_CAPTURE);<br />
print_r($stack);<br />
?&gt;</p></blockquote>
<p>This prints:<br />
Array<br />
(<br />
[0] =&gt; 3<br />
[1] =&gt; +<br />
[2] =&gt; 2<br />
[3] =&gt; /<br />
[4] =&gt; 7<br />
[5] =&gt; -<br />
[6] =&gt; 9<br />
)</p>
<p>The separator regular expression looks for the four mathematical operators (+, -, /, *), surrounded by optional leading or trailing spaces. The PREG_SPLIT_DELIM_CAPTURE flag tells <em>preg_split( )</em> to include the matches as part of the separator regular expression in parentheses in the returned array of strings. Only the mathematical operator character class is in parentheses, so the returned array doesn&#8217;t have any spaces in it.</p>
]]></content:encoded>
			<wfw:commentRss>http://vdiscussion.com/how-to-break-a-string-into-pieces-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create Comma Separated Data in PHP</title>
		<link>http://vdiscussion.com/how-to-create-comma-separated-data-in-php/</link>
		<comments>http://vdiscussion.com/how-to-create-comma-separated-data-in-php/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 18:22:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Programs]]></category>
		<category><![CDATA[Theory Subjects]]></category>
		<category><![CDATA[Create Comma Separated]]></category>

		<guid isPermaLink="false">http://vdiscussion.com/?p=127</guid>
		<description><![CDATA[Use the fputcsv( ) function to generate a CSV-formatted line from an array of data.  Writes the data in $sales into a file Sample Program &#60;?php $sales = array( array(&#8216;Northeast&#8217;,&#8217;2005-01-01&#8242;,&#8217;2005-02-01&#8242;,12.54), array(&#8216;Northwest&#8217;,&#8217;2005-01-01&#8242;,&#8217;2005-02-01&#8242;,546.33), array(&#8216;Southeast&#8217;,&#8217;2005-01-01&#8242;,&#8217;2005-02-01&#8242;,93.26), array(&#8216;Southwest&#8217;,&#8217;2005-01-01&#8242;,&#8217;2005-02-01&#8242;,945.21), array(&#8216;All Regions&#8217;,'&#8211;&#8217;,'&#8211;&#8217;,1597.34) ); ob_start(); $fh = fopen(&#8216;php://output&#8217;,'w&#8217;) or die(&#8220;Can&#8217;t open php://output&#8221;); foreach ($sales as $sales_line) { if (fputcsv($fh, $sales_line) === false) { [...]]]></description>
			<content:encoded><![CDATA[<p>Use the <em>fputcsv( )</em> function to generate a CSV-formatted line from an array of data.  Writes the data in $sales into a file</p>
<p><strong>Sample Program</strong></p>
<blockquote><p>&lt;?php<br />
$sales = array( array(&#8216;Northeast&#8217;,&#8217;2005-01-01&#8242;,&#8217;2005-02-01&#8242;,12.54),<br />
array(&#8216;Northwest&#8217;,&#8217;2005-01-01&#8242;,&#8217;2005-02-01&#8242;,546.33),<br />
array(&#8216;Southeast&#8217;,&#8217;2005-01-01&#8242;,&#8217;2005-02-01&#8242;,93.26),<br />
array(&#8216;Southwest&#8217;,&#8217;2005-01-01&#8242;,&#8217;2005-02-01&#8242;,945.21),<br />
array(&#8216;All Regions&#8217;,'&#8211;&#8217;,'&#8211;&#8217;,1597.34) );<br />
ob_start();<br />
$fh = fopen(&#8216;php://output&#8217;,'w&#8217;) or die(&#8220;Can&#8217;t open php://output&#8221;);<br />
foreach ($sales as $sales_line) {<br />
if (fputcsv($fh, $sales_line) === false) {<br />
die(&#8220;Can&#8217;t write CSV line&#8221;);<br />
}<br />
}<br />
fclose($fh) or die(&#8220;Can&#8217;t close php://output&#8221;);<br />
$output = ob_get_contents();<br />
ob_end_clean();<br />
?&gt;</p></blockquote>
<blockquote><p>&lt;?php<br />
$sales = array( array(&#8216;Northeast&#8217;,&#8217;2005-01-01&#8242;,&#8217;2005-02-01&#8242;,12.54),<br />
array(&#8216;Northwest&#8217;,&#8217;2005-01-01&#8242;,&#8217;2005-02-01&#8242;,546.33),<br />
array(&#8216;Southeast&#8217;,&#8217;2005-01-01&#8242;,&#8217;2005-02-01&#8242;,93.26),<br />
array(&#8216;Southwest&#8217;,&#8217;2005-01-01&#8242;,&#8217;2005-02-01&#8242;,945.21),<br />
array(&#8216;All Regions&#8217;,'&#8211;&#8217;,'&#8211;&#8217;,1597.34) );</p>
<p>$fh = fopen(&#8216;sales.csv&#8217;,'w&#8217;) or die(&#8220;Can&#8217;t open sales.csv&#8221;);<br />
foreach ($sales as $sales_line) {<br />
if (fputcsv($fh, $sales_line) === false) {<br />
die(&#8220;Can&#8217;t write CSV line&#8221;);<br />
}<br />
}<br />
fclose($fh) or die(&#8220;Can&#8217;t close sales.csv&#8221;);<br />
?&gt;</p></blockquote>
<p>Putting comma-separated data into a string</p>
<p>Printing comma-separated data</p>
<blockquote>
<pre>&lt;?php $sales = array( array('Northeast','2005-01-01','2005-02-01',12.54),</pre>
<pre>                array('Northwest','2005-01-01','2005-02-01',546.33),</pre>
<pre>                array('Southeast','2005-01-01','2005-02-01',93.26),</pre>
<pre>                array('Southwest','2005-01-01','2005-02-01',945.21),</pre>
<pre>                array('All Regions','--','--',1597.34) );</pre>
<pre>$fh = fopen('php://output','w');</pre>
<pre>foreach ($sales as $sales_line) {</pre>
<pre>    if (fputcsv($fh, $sales_line) === false) {</pre>
<pre>        die("Can't write CSV line");</pre>
<pre>    }</pre>
<pre>}</pre>
<pre>fclose($fh);</pre>
<pre>?&gt;</pre>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://vdiscussion.com/how-to-create-comma-separated-data-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Trim Blanks from a String using PHP</title>
		<link>http://vdiscussion.com/how-to-trim-blanks-from-a-string-using-php/</link>
		<comments>http://vdiscussion.com/how-to-trim-blanks-from-a-string-using-php/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 18:12:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Programs]]></category>
		<category><![CDATA[Theory Subjects]]></category>
		<category><![CDATA[chomp( )]]></category>
		<category><![CDATA[chop( )]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[ltrim( )]]></category>
		<category><![CDATA[rtrim( )]]></category>
		<category><![CDATA[trim( )]]></category>

		<guid isPermaLink="false">http://vdiscussion.com/?p=123</guid>
		<description><![CDATA[Use ltrim( ), rtrim( ), or trim( ), ltrim( ) removes whitespace from the beginning of a string, rtrim( ) from the end of a string, and trim( ) from both the beginning and end of a string: &#60;?php $zipcode = trim($_REQUEST['zipcode']); $no_linefeed = rtrim($_REQUEST['text']); $name = ltrim($_REQUEST['name']); ?&#62; For these functions, whitespace is defined [...]]]></description>
			<content:encoded><![CDATA[<p>Use <em>ltrim( ), rtrim( )</em>, or <em>trim( ),</em> <em>ltrim( )</em> removes whitespace from the beginning of a string, <em>rtrim( )</em> from the end of a string, and <em>trim( )</em> from both the beginning and end of a string:</p>
<blockquote>
<pre>&lt;?php
$zipcode = trim($_REQUEST['zipcode']);
$no_linefeed = rtrim($_REQUEST['text']);
$name = ltrim($_REQUEST['name']);
?&gt;</pre>
</blockquote>
<pre>For these functions, whitespace is defined as the following
characters:newline, carriage return, space, horizontal and
vertical tab, and null .</pre>
<p>Trimming whitespace off of strings saves storage space and canmake for more<br />
precise display of formatted data or text within<tt>&lt;pre&gt;</tt> tags, for example. If<br />
you are doing comparisons with userinput, you should trim the data first, so<br />
that someone who mistakenly enters&#8221;98052&#8243; as their zip code isn&#8217;t forced to<br />
fix an error that really isn&#8217;t one. Trimming before exact text comparisons<br />
also ensures that, for example, &#8220;salami\n&#8221; equals &#8220;salami.&#8221; It&#8217;s also a good<br />
idea to normalize string data bytrimming it before storing it in a database.</p>
<p>The <em>trim( )</em> functions can also remove user-specifiedcharacters from strings.<br />
Pass the characters you want to remove as a secondargument. You can indicate<br />
a range of characters with two dots between the firstand last characters in<br />
the range:</p>
<blockquote>
<pre>&lt;?php
// Remove numerals and space from the beginning of the line
print ltrim('10 PRINT A$',' 0..9');
// Remove semicolon from the end of the line
print rtrim('SELECT * FROM turtles;',';');
?&gt;</pre>
</blockquote>
<p>This prints:</p>
<blockquote>
<pre>PRINT A$</pre>
<pre>SELECT * FROM turtles</pre>
</blockquote>
<p>PHP also provides <em>chop( ) </em>asan alias for <em>rtrim( )</em>. However, you&#8217;re best off<br />
using <em>rtrim( )</em> instead because PHP&#8217;s <em>chop( )</em> behaves differently than Perl&#8217;s<br />
<em>chop( )</em> (which is deprecated in favor of<em> chomp( )</em> , anyway), and using it can<br />
confuse others when they read your code.</p>
]]></content:encoded>
			<wfw:commentRss>http://vdiscussion.com/how-to-trim-blanks-from-a-string-using-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
