<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="http://commons.oreilly.com/wiki/skins/common/feed.css?97"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title>PHP Cookbook/Strings - Revision history</title>
		<link>http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Strings&amp;action=history</link>
		<description>Revision history for this page on the wiki</description>
		<language>en</language>
		<generator>MediaWiki 1.11.0</generator>
		<lastBuildDate>Mon, 20 May 2013 17:58:42 GMT</lastBuildDate>
		<item>
			<title>Newacct: /* Solution */</title>
			<link>http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Strings&amp;diff=26127&amp;oldid=prev</link>
			<description>&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Solution&lt;/span&gt;&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 22:28, 29 January 2010&lt;/td&gt;
			&lt;/tr&gt;
		&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 260:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 260:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; for ($i = 0, $j = strlen($string); $i &amp;lt; $j; $i++) {&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; for ($i = 0, $j = strlen($string); $i &amp;lt; $j; $i++) {&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;     if (strstr('aeiouAEIOU',$string[$i])) {&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;     if (strstr('aeiouAEIOU',$string[$i])) {&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;         $vowels++;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;     }&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; }&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Alternately, you can split the string into characters using &amp;lt;tt&amp;gt;str_split( )&amp;lt;/tt&amp;gt;, and then loop over them using a &amp;lt;tt&amp;gt;foreach&amp;lt;/tt&amp;gt; loop:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; $string = &amp;quot;This weekend, I'm going shopping for a pet chicken.&amp;quot;;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; $vowels = 0;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt; foreach (str_split($string) as $c) {&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;     if (strstr('aeiouAEIOU',$c)) {&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;         $vowels++;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;         $vowels++;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;     }&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;     }&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key wikicontent:diff:version:1.11a:oldid:7313:newid:26127 --&gt;
&lt;/table&gt;</description>
			<pubDate>Fri, 29 Jan 2010 22:28:10 GMT</pubDate>			<dc:creator>Newacct</dc:creator>			<comments>http://commons.oreilly.com/wiki/index.php/Talk:PHP_Cookbook/Strings</comments>		</item>
		<item>
			<title>Docbook2Wiki: Initial conversion from Docbook</title>
			<link>http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Strings&amp;diff=7313&amp;oldid=prev</link>
			<description>&lt;p&gt;Initial conversion from Docbook&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 13:36, 7 March 2008&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/table&gt;</description>
			<pubDate>Fri, 07 Mar 2008 13:36:04 GMT</pubDate>			<dc:creator>Docbook2Wiki</dc:creator>			<comments>http://commons.oreilly.com/wiki/index.php/Talk:PHP_Cookbook/Strings</comments>		</item>
		<item>
			<title>Evanlenz: 1 revision(s)</title>
			<link>http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Strings&amp;diff=3105&amp;oldid=prev</link>
			<description>&lt;p&gt;1 revision(s)&lt;/p&gt;

			&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;tr&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 22:29, 6 March 2008&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/table&gt;</description>
			<pubDate>Thu, 06 Mar 2008 22:29:56 GMT</pubDate>			<dc:creator>Evanlenz</dc:creator>			<comments>http://commons.oreilly.com/wiki/index.php/Talk:PHP_Cookbook/Strings</comments>		</item>
		<item>
			<title>Docbook2Wiki: Initial conversion from Docbook</title>
			<link>http://commons.oreilly.com/wiki/index.php?title=PHP_Cookbook/Strings&amp;diff=3104&amp;oldid=prev</link>
			<description>&lt;p&gt;Initial conversion from Docbook&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{PHP Cookbook/TOC}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Strings in PHP are a sequence of characters, such as &amp;quot;We hold these truths to be self evident,&amp;quot; or &amp;quot;Once upon a time,&amp;quot; or even &amp;quot;111211211.&amp;quot; When you read data from a file or output it to a web browser, your data is represented as strings.&lt;br /&gt;
&lt;br /&gt;
Individual characters in strings can be referenced with array subscript style notation, as in C. The first character in the string is at index 0. For example:&lt;br /&gt;
&lt;br /&gt;
 $neighbor = 'Hilda';&lt;br /&gt;
 print $neighbor[3];&lt;br /&gt;
 '''d'''&lt;br /&gt;
          &lt;br /&gt;
&lt;br /&gt;
However, PHP strings differ from C strings in that they are binary-safe (i.e., they can contain null bytes) and can grow and shrink on demand. Their size is limited only by the amount of memory that is available.&lt;br /&gt;
&lt;br /&gt;
You can initialize strings three ways, similar in form and behavior to Perl and the Unix shell: with single quotes, with double quotes, and with the &amp;quot;here document&amp;quot; (heredoc) format. With single-quoted strings, the only special characters you need to escape inside a string are backslash and the single quote itself:&lt;br /&gt;
&lt;br /&gt;
 print 'I have gone to the store.';&lt;br /&gt;
 print 'I\'ve gone to the store.';&lt;br /&gt;
 print 'Would you pay $1.75 for 8 ounces of tap water?';&lt;br /&gt;
 print 'In double-quoted strings, newline is represented by \n';&lt;br /&gt;
 '''I have gone to the store.'''&lt;br /&gt;
             '''I've gone to the store.'''&lt;br /&gt;
             '''Would you pay $1.75 for 8 ounces of tap water?'''&lt;br /&gt;
             '''In double-quoted strings, newline is represented by \n'''&lt;br /&gt;
          &lt;br /&gt;
&lt;br /&gt;
Because PHP doesn't check for variable interpolation or almost any escape sequences in single-quoted strings, defining strings this way is straightforward and fast.&lt;br /&gt;
&lt;br /&gt;
Double-quoted strings don't recognize escaped single quotes, but they do recognize interpolated variables and the escape sequences shown in [[PHP Cookbook/Strings#phpckbk-CHP-1-TABLE-1|Table 1-1]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;phpckbk-CHP-1-TABLE-1&amp;quot;&amp;gt;&lt;br /&gt;
'''Table 1-1. Double-quoted string escape sequences'''&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; &lt;br /&gt;
|-&lt;br /&gt;
! Escape sequence !! Character&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;\n&amp;lt;/tt&amp;gt; || Newline (ASCII 10)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;\r&amp;lt;/tt&amp;gt; || Carriage return (ASCII 13)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;\t&amp;lt;/tt&amp;gt; || Tab (ASCII 9)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;\\&amp;lt;/tt&amp;gt; || Backslash&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;\$&amp;lt;/tt&amp;gt; || Dollar sign&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;\&amp;lt;/tt&amp;gt; &amp;quot; || Double quotes&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;\{&amp;lt;/tt&amp;gt; || Left brace&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;\}&amp;lt;/tt&amp;gt; || Right brace&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;\[&amp;lt;/tt&amp;gt; || Left bracket&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;\]&amp;lt;/tt&amp;gt; || Right bracket&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;\0&amp;lt;/tt&amp;gt; through &amp;lt;tt&amp;gt;\777&amp;lt;/tt&amp;gt; || Octal value&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;\x0&amp;lt;/tt&amp;gt; through &amp;lt;tt&amp;gt;\xFF&amp;lt;/tt&amp;gt; || Hex value&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;I've gone to the store.&amp;quot;;&lt;br /&gt;
 print &amp;quot;The sauce cost \$10.25.&amp;quot;;&lt;br /&gt;
 $cost = '$10.25';&lt;br /&gt;
 print &amp;quot;The sauce cost $cost.&amp;quot;;&lt;br /&gt;
 print &amp;quot;The sauce cost \$\061\060.\x32\x35.&amp;quot;;&lt;br /&gt;
 '''I've gone to the store.'''&lt;br /&gt;
             '''The sauce cost $10.25.'''&lt;br /&gt;
             '''The sauce cost $10.25.'''&lt;br /&gt;
             '''The sauce cost $10.25.'''&lt;br /&gt;
          &lt;br /&gt;
&lt;br /&gt;
The last line of code prints the price of sauce correctly because the character &amp;lt;tt&amp;gt;1&amp;lt;/tt&amp;gt; is ASCII code 49 decimal and 061 octal. Character &amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt; is ASCII 48 decimal and 060 octal; &amp;lt;tt&amp;gt;2&amp;lt;/tt&amp;gt; is ASCII 50 decimal and 32 hex; and &amp;lt;tt&amp;gt;5&amp;lt;/tt&amp;gt; is ASCII 53 decimal and 35 hex.&lt;br /&gt;
&lt;br /&gt;
Heredoc-specified strings recognize all the interpolations and escapes of double- quoted strings, but they don't require double quotes to be escaped. Heredocs start with &amp;lt;tt&amp;gt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;/tt&amp;gt; and a token. That token (with no leading or trailing whitespace), followed by a semicolon to end the statement (if necessary), ends the heredoc. For example:&lt;br /&gt;
&lt;br /&gt;
 print &amp;lt;&amp;lt;&amp;lt; END&lt;br /&gt;
 It's funny when signs say things like:&lt;br /&gt;
    Original &amp;quot;Root&amp;quot; Beer&lt;br /&gt;
    &amp;quot;Free&amp;quot; Gift&lt;br /&gt;
    Shoes cleaned while &amp;quot;you&amp;quot; wait&lt;br /&gt;
 or have other misquoted words.&lt;br /&gt;
 END;&lt;br /&gt;
 '''It's funny when signs say things like:'''&lt;br /&gt;
             '''Original &amp;quot;Root&amp;quot; Beer'''&lt;br /&gt;
             '''   &amp;quot;Free&amp;quot; Gift'''&lt;br /&gt;
             '''   Shoes cleaned while &amp;quot;you&amp;quot; wait'''&lt;br /&gt;
             '''or have other misquoted words.'''&lt;br /&gt;
          &lt;br /&gt;
&lt;br /&gt;
With heredocs, newlines, spacing, and quotes are all preserved. The end-of-string identifier is usually all caps, by convention, and it is case sensitive. Thus, this is okay:&lt;br /&gt;
&lt;br /&gt;
 print &amp;lt;&amp;lt;&amp;lt; PARSLEY&lt;br /&gt;
 It's easy to grow fresh:&lt;br /&gt;
 Parsley&lt;br /&gt;
 Chives&lt;br /&gt;
 on your windowsill&lt;br /&gt;
 PARSLEY;&lt;br /&gt;
&lt;br /&gt;
So is this:&lt;br /&gt;
&lt;br /&gt;
 print &amp;lt;&amp;lt;&amp;lt; DOGS&lt;br /&gt;
 If you like pets, yell out:&lt;br /&gt;
 DOGS AND CATS ARE GREAT!&lt;br /&gt;
 DOGS;&lt;br /&gt;
&lt;br /&gt;
Heredocs are useful for printing out HTML with interpolated variables:&lt;br /&gt;
&lt;br /&gt;
 if ($remaining_cards &amp;gt; 0) {&lt;br /&gt;
     $url = '/deal.php';&lt;br /&gt;
     $text = 'Deal More Cards';&lt;br /&gt;
 } else {&lt;br /&gt;
     $url = '/new-game.php';&lt;br /&gt;
     $text = 'Start a New Game';&lt;br /&gt;
 }&lt;br /&gt;
 print &amp;lt;&amp;lt;&amp;lt; HTML&lt;br /&gt;
 There are &amp;amp;lt;b&amp;gt;$remaining_cards&amp;amp;lt;/b&amp;gt; left.&lt;br /&gt;
 &amp;amp;lt;p&amp;gt;&lt;br /&gt;
 &amp;lt;a href=&amp;quot;$url&amp;quot;&amp;gt;$text&amp;lt;/a&amp;gt;&lt;br /&gt;
 HTML;&lt;br /&gt;
&lt;br /&gt;
In this case, the semicolon needs to go after the end-of-string delimiter, to tell PHP the statement is ended. In some cases, however, you shouldn't use the semicolon:&lt;br /&gt;
&lt;br /&gt;
 $a = &amp;lt;&amp;lt;&amp;lt; END&lt;br /&gt;
 Once upon a time, there was a&lt;br /&gt;
 END&lt;br /&gt;
 . ' boy!';&lt;br /&gt;
 print $a;&lt;br /&gt;
 '''Once upon a time, there was a boy!'''&lt;br /&gt;
          &lt;br /&gt;
&lt;br /&gt;
In this case, the expression needs to continue on the next line, so you don't use a semicolon. Note also that in order for PHP to recognize the end-of-string delimiter, the &amp;lt;tt&amp;gt;.&amp;lt;/tt&amp;gt; string concatenation operator needs to go on a separate line from the end-of-string delimiter.&lt;br /&gt;
&lt;br /&gt;
== Accessing Substrings ==&lt;br /&gt;
&lt;br /&gt;
You want to extract part of a string, starting at a particular place in the string. For example, you want the first eight characters of a username entered into a form.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;substr( )&amp;lt;/tt&amp;gt; to select your substrings:&lt;br /&gt;
&lt;br /&gt;
 $substring = substr($string,$start,$length);&lt;br /&gt;
 $username = substr($_REQUEST['username'],0,8);&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;tt&amp;gt;$start&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;$length&amp;lt;/tt&amp;gt; are positive, &amp;lt;tt&amp;gt;substr( )&amp;lt;/tt&amp;gt; returns &amp;lt;tt&amp;gt;$length&amp;lt;/tt&amp;gt; characters in the string, starting at &amp;lt;tt&amp;gt;$start&amp;lt;/tt&amp;gt;. The first character in the string is at position 0:&lt;br /&gt;
&lt;br /&gt;
 print substr('watch out for that tree',6,5);&lt;br /&gt;
 '''out f'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
If you leave out &amp;lt;tt&amp;gt;$length&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;substr( )&amp;lt;/tt&amp;gt; returns the string from &amp;lt;tt&amp;gt;$start&amp;lt;/tt&amp;gt; to the end of the original string:&lt;br /&gt;
&lt;br /&gt;
 print substr('watch out for that tree',17);&lt;br /&gt;
 '''t tree'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
If &amp;lt;tt&amp;gt;$start&amp;lt;/tt&amp;gt; plus &amp;lt;tt&amp;gt;$length&amp;lt;/tt&amp;gt; goes past the end of the string, &amp;lt;tt&amp;gt;substr( )&amp;lt;/tt&amp;gt; returns all of the string from &amp;lt;tt&amp;gt;$start&amp;lt;/tt&amp;gt; forward:&lt;br /&gt;
&lt;br /&gt;
 print substr('watch out for that tree',20,5);&lt;br /&gt;
 '''ree'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
If &amp;lt;tt&amp;gt;$start&amp;lt;/tt&amp;gt; is negative, &amp;lt;tt&amp;gt;substr( )&amp;lt;/tt&amp;gt; counts back from the end of the string to determine where your substring starts:&lt;br /&gt;
&lt;br /&gt;
 print substr('watch out for that tree',-6);&lt;br /&gt;
 print substr('watch out for that tree',-17,5);&lt;br /&gt;
 '''t tree'''&lt;br /&gt;
                '''out f'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
If &amp;lt;tt&amp;gt;$length&amp;lt;/tt&amp;gt; is negative, &amp;lt;tt&amp;gt;substr( )&amp;lt;/tt&amp;gt; counts back from the end of the string to determine where your substring ends:&lt;br /&gt;
&lt;br /&gt;
 print substr('watch out for that tree',15,-2);&lt;br /&gt;
 print substr('watch out for that tree',-4,-1);&lt;br /&gt;
 '''hat tr'''&lt;br /&gt;
                '''tre'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on &amp;lt;tt&amp;gt;substr( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/substr''.&lt;br /&gt;
&lt;br /&gt;
== Replacing Substrings ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to replace a substring with a different string. For example, you want to obscure all but the last four digits of a credit card number before printing it.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;substr_replace( )&amp;lt;/tt&amp;gt; :&lt;br /&gt;
&lt;br /&gt;
 // Everything from position $start to the end of $old_string&lt;br /&gt;
 // becomes $new_substring&lt;br /&gt;
 $new_string = substr_replace($old_string,$new_substring,$start);&lt;br /&gt;
 &lt;br /&gt;
 // $length characters, starting at position $start, become $new_substring&lt;br /&gt;
 $new_string = substr_replace($old_string,$new_substring,$start,$length);&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Without the &amp;lt;tt&amp;gt;$length&amp;lt;/tt&amp;gt; argument, &amp;lt;tt&amp;gt;substr_replace( )&amp;lt;/tt&amp;gt; replaces everything from &amp;lt;tt&amp;gt;$start&amp;lt;/tt&amp;gt; to the end of the string. If &amp;lt;tt&amp;gt;$length&amp;lt;/tt&amp;gt; is specified, only that many characters are replaced:&lt;br /&gt;
&lt;br /&gt;
 print substr_replace('My pet is a blue dog.','fish.',12);&lt;br /&gt;
 print substr_replace('My pet is a blue dog.','green',12,4);&lt;br /&gt;
 $credit_card = '4111 1111 1111 1111';&lt;br /&gt;
 print substr_replace($credit_card,'xxxx ',0,strlen($credit_card)-4);&lt;br /&gt;
 '''My pet is a fish.'''&lt;br /&gt;
                '''My pet is a green dog.'''&lt;br /&gt;
                '''xxxx 1111'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
If &amp;lt;tt&amp;gt;$start&amp;lt;/tt&amp;gt; is negative, the new substring is placed at &amp;lt;tt&amp;gt;$start&amp;lt;/tt&amp;gt; characters counting from the end of &amp;lt;tt&amp;gt;$old_string&amp;lt;/tt&amp;gt;, not from the beginning:&lt;br /&gt;
&lt;br /&gt;
 print substr_replace('My pet is a blue dog.','fish.',-9);&lt;br /&gt;
 print substr_replace('My pet is a blue dog.','green',-9,4);&lt;br /&gt;
 '''My pet is a fish.'''&lt;br /&gt;
                '''My pet is a green dog.'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
If &amp;lt;tt&amp;gt;$start&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;$length&amp;lt;/tt&amp;gt; are 0, the new substring is inserted at the start of &amp;lt;tt&amp;gt;$old_string&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 print substr_replace('My pet is a blue dog.','Title: ',0,0);&lt;br /&gt;
 '''Title: My pet is a blue dog'''.&lt;br /&gt;
&lt;br /&gt;
The function &amp;lt;tt&amp;gt;substr_replace( )&amp;lt;/tt&amp;gt; is useful when you've got text that's too big to display all at once, and you want to display some of the text with a link to the rest. For example, this displays the first 25 characters of a message with an ellipsis after it as a link to a page that displays more text:&lt;br /&gt;
&lt;br /&gt;
 $r = mysql_query(&amp;quot;SELECT id,message FROM messages WHERE id = $id&amp;quot;) or die( );&lt;br /&gt;
 $ob = mysql_fetch_object($r);&lt;br /&gt;
 printf('&amp;lt;a href=&amp;quot;more-text.php?id=%d&amp;quot;&amp;gt;%s&amp;lt;/a&amp;gt;',&lt;br /&gt;
        $ob-&amp;gt;id, substr_replace($ob-&amp;gt;message,' ...',25));&lt;br /&gt;
&lt;br /&gt;
The ''more-text.php'' page can use the message ID passed in the query string to retrieve the full message and display it.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on &amp;lt;tt&amp;gt;substr_replace( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/substr-replace''.&lt;br /&gt;
&lt;br /&gt;
== Processing a String One Character at a Time ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You need to process each character in a string individually.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Loop through each character in the string with &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt;. This example counts the vowels in a string:&lt;br /&gt;
&lt;br /&gt;
 $string = &amp;quot;This weekend, I'm going shopping for a pet chicken.&amp;quot;;&lt;br /&gt;
 $vowels = 0;&lt;br /&gt;
 for ($i = 0, $j = strlen($string); $i &amp;lt; $j; $i++) {&lt;br /&gt;
     if (strstr('aeiouAEIOU',$string[$i])) {&lt;br /&gt;
         $vowels++;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Processing a string a character at a time is an easy way to calculate the &amp;quot;Look and Say&amp;quot; sequence:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;function lookandsay($s) {&lt;br /&gt;
    // initialize the return value to the empty string&lt;br /&gt;
    $r = '';&lt;br /&gt;
    /* $m holds the character we're counting, initialize to the first&lt;br /&gt;
    // character in the string*/&lt;br /&gt;
    $m = $s[0];&lt;br /&gt;
    // $n is the number of $m's we've seen, initialize to 1&lt;br /&gt;
    $n = 1;&lt;br /&gt;
    for ($i = 1, $j = strlen($s); $i &amp;lt; $j; $i++) {&lt;br /&gt;
        // if this character is the same as the last one&lt;br /&gt;
        if ($s[$i] == $m) {&lt;br /&gt;
            // increment the count of this character&lt;br /&gt;
            $n++;&lt;br /&gt;
        } else {&lt;br /&gt;
            // otherwise, add the count and character to the return value &lt;br /&gt;
            $r .= $n.$m;&lt;br /&gt;
            // set the character we're looking for to the current one &lt;br /&gt;
            $m = $s[$i];&lt;br /&gt;
            // and reset the count to 1&lt;br /&gt;
            $n = 1;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    // return the built up string as well as the last count and character&lt;br /&gt;
    return $r.$n.$m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
for ($i = 0, $s = 1; $i &amp;lt; 10; $i++) {&lt;br /&gt;
    $s = lookandsay($s);&lt;br /&gt;
    print &amp;quot;$s\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;'''1'''&lt;br /&gt;
                '''11'''&lt;br /&gt;
                '''21'''&lt;br /&gt;
                '''1211'''&lt;br /&gt;
                '''111221'''&lt;br /&gt;
                '''312211'''&lt;br /&gt;
                '''13112221'''&lt;br /&gt;
                '''1113213211'''&lt;br /&gt;
                '''31131211131221'''&lt;br /&gt;
                '''13211311123113112211'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
It's called the &amp;quot;Look and Say&amp;quot; sequence because each element is what you get by looking at the previous element and saying what's in it. For example, looking at the first element, 1, you say &amp;quot;one one.&amp;quot; So the second element is &amp;quot;11.&amp;quot; That's two ones, so the third element is &amp;quot;21.&amp;quot; Similarly, that's one two and one one, so the fourth element is &amp;quot;1211,&amp;quot; and so on.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; at ''http://www.php.net/for''; more about the &amp;quot;Look and Say&amp;quot; sequence at ''http://mathworld.wolfram.com/LookandSaySequence.html''.&lt;br /&gt;
&lt;br /&gt;
== Reversing a String by Word or Character ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to reverse the words or the characters in a string.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;strrev( )&amp;lt;/tt&amp;gt; to reverse by character:&lt;br /&gt;
&lt;br /&gt;
 print strrev('This is not a palindrome.');&lt;br /&gt;
 '''.emordnilap a ton si sihT'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
To reverse by words, explode the string by word boundary, reverse the words, then rejoin:&lt;br /&gt;
&lt;br /&gt;
 $s = &amp;quot;Once upon a time there was a turtle.&amp;quot;;&lt;br /&gt;
 // break the string up into words&lt;br /&gt;
 $words = explode(' ',$s);&lt;br /&gt;
 // reverse the array of words&lt;br /&gt;
 $words = array_reverse($words);&lt;br /&gt;
 // rebuild the string&lt;br /&gt;
 $s = join(' ',$words);&lt;br /&gt;
 print $s;&lt;br /&gt;
 '''turtle. a was there time a upon Once'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Reversing a string by words can also be done all in one line:&lt;br /&gt;
&lt;br /&gt;
 $reversed_s = join(' ',array_reverse(explode(' ',$s)));&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
[[PHP Cookbook/Files#Processing Every Word in a File|Recipe 18.8]] discusses the implications of using something other than a space character as your word boundary; documentation on &amp;lt;tt&amp;gt;strrev( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/strrev'' and &amp;lt;tt&amp;gt;array_reverse( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/array-reverse''.&lt;br /&gt;
&lt;br /&gt;
== Expanding and Compressing Tabs ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to change spaces to tabs (or tabs to spaces) in a string while keeping text aligned with tab stops. For example, you want to display formatted text to users in a standardized way.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;str_replace( )&amp;lt;/tt&amp;gt; to switch spaces to tabs or tabs to spaces:&lt;br /&gt;
&lt;br /&gt;
 $r = mysql_query(&amp;quot;SELECT message FROM messages WHERE id = 1&amp;quot;) or die();&lt;br /&gt;
 $ob = mysql_fetch_object($r);&lt;br /&gt;
 $tabbed = str_replace(' ',&amp;quot;\t&amp;quot;,$ob-&amp;gt;message);&lt;br /&gt;
 $spaced = str_replace(&amp;quot;\t&amp;quot;,' ',$ob-&amp;gt;message);&lt;br /&gt;
 &lt;br /&gt;
 print &amp;quot;With Tabs: &amp;amp;lt;pre&amp;gt;$tabbed&amp;amp;lt;/pre&amp;gt;&amp;quot;;&lt;br /&gt;
 print &amp;quot;With Spaces: &amp;amp;lt;pre&amp;gt;$spaced&amp;amp;lt;/pre&amp;gt;&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
Using &amp;lt;tt&amp;gt;str_replace( )&amp;lt;/tt&amp;gt; for conversion, however, doesn't respect tab stops. If you want tab stops every eight characters, a line beginning with a five-letter word and a tab should have that tab replaced with three spaces, not one. Use the &amp;lt;tt&amp;gt;pc_tab_expand( )&amp;lt;/tt&amp;gt; function shown in [[PHP Cookbook/Strings#phpckbk-CHP-1-EX-1|Example 1-1]] to turn tabs to spaces in a way that respects tab stops.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;phpckbk-CHP-1-EX-1&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 1-1. pc_tab_expand( )'''&lt;br /&gt;
&lt;br /&gt;
 function pc_tab_expand($a) {&lt;br /&gt;
   $tab_stop = 8;&lt;br /&gt;
   while (strstr($a,&amp;quot;\t&amp;quot;)) {&lt;br /&gt;
     $a = preg_replace('/^([^\t]*)(\t+)/e',&lt;br /&gt;
                       &amp;quot;'\\1'.str_repeat(' ',strlen('\\2') * &lt;br /&gt;
                        $tab_stop - strlen('\\1') % $tab_stop)&amp;quot;,$a);&lt;br /&gt;
   } &lt;br /&gt;
   return $a;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 $spaced = pc_tab_expand($ob-&amp;gt;message);&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can use the &amp;lt;tt&amp;gt;pc_tab_unexpand()&amp;lt;/tt&amp;gt; function shown in [[PHP Cookbook/Strings#phpckbk-CHP-1-EX-2|Example 1-2]] to turn spaces back to tabs.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;phpckbk-CHP-1-EX-2&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 1-2. pc_tab_unexpand( )'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;function pc_tab_unexpand($x) {&lt;br /&gt;
  $tab_stop = 8;&lt;br /&gt;
  &lt;br /&gt;
  $lines = explode(&amp;quot;\n&amp;quot;,$x);&lt;br /&gt;
  for ($i = 0, $j = count($lines); $i &amp;lt; $j; $i++) {&lt;br /&gt;
    $lines[$i] = pc_tab_expand($lines[$i]);&lt;br /&gt;
    $e = preg_split(&amp;quot;/(.\{$tab_stop})/&amp;quot;,$lines[$i],-1,PREG_SPLIT_DELIM_CAPTURE);&lt;br /&gt;
    $lastbit = array_pop($e);&lt;br /&gt;
    if (!isset($lastbit)) { $lastbit = ''; }&lt;br /&gt;
    if ($lastbit == str_repeat(' ',$tab_stop)) { $lastbit = &amp;quot;\t&amp;quot;; }&lt;br /&gt;
    for ($m = 0, $n = count($e); $m &amp;lt; $n; $m++) {&lt;br /&gt;
      $e[$m] = preg_replace('/  +$',&amp;quot;\t&amp;quot;,$e[$m]);&lt;br /&gt;
    }&lt;br /&gt;
    $lines[$i] = join('',$e).$lastbit;&lt;br /&gt;
  }&lt;br /&gt;
  $x = join(&amp;quot;\n&amp;quot;, $lines);&lt;br /&gt;
  return $x;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$tabbed = pc_tab_unexpand($ob-&amp;gt;message);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Both functions take a string as an argument and return the string appropriately modified.&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Each function assumes tab stops are every eight spaces, but that can be modified by changing the setting of the &amp;lt;tt&amp;gt;$tab_stop&amp;lt;/tt&amp;gt; variable.&lt;br /&gt;
&lt;br /&gt;
The regular expression in &amp;lt;tt&amp;gt;pc_tab_expand( )&amp;lt;/tt&amp;gt; matches both a group of tabs and all the text in a line before that group of tabs. It needs to match the text before the tabs because the length of that text affects how many spaces the tabs should be replaced so that subsequent text is aligned with the next tab stop. The function doesn't just replace each tab with eight spaces; it adjusts text after tabs to line up with tab stops.&lt;br /&gt;
&lt;br /&gt;
Similarly, &amp;lt;tt&amp;gt;pc_tab_unexpand( )&amp;lt;/tt&amp;gt; doesn't just look for eight consecutive spaces and then replace them with one tab character. It divides up each line into eight-character chunks and then substitutes ending whitespace in those chunks (at least two spaces) with tabs. This not only preserves text alignment with tab stops; it also saves space in the string.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on &amp;lt;tt&amp;gt;str_replace( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/str-replace''.&lt;br /&gt;
&lt;br /&gt;
== Controlling Case ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You need to capitalize, lowercase, or otherwise modify the case of letters in a string. For example, you want to capitalize the initial letters of names but lowercase the rest.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;ucfirst( )&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;ucwords( )&amp;lt;/tt&amp;gt; to capitalize the first letter of one or more words:&lt;br /&gt;
&lt;br /&gt;
 print ucfirst(&amp;quot;how do you do today?&amp;quot;);&lt;br /&gt;
 print ucwords(&amp;quot;the prince of wales&amp;quot;);&lt;br /&gt;
 '''How do you do today?'''&lt;br /&gt;
                '''The Prince Of Wales'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;strtolower( )&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;strtoupper( )&amp;lt;/tt&amp;gt; to modify the case of entire strings:&lt;br /&gt;
&lt;br /&gt;
 print strtoupper(&amp;quot;i'm not yelling!&amp;quot;);&lt;br /&gt;
 // Tags must be lowercase to be XHTML compliant&lt;br /&gt;
 print strtolower('&amp;lt;A HREF=&amp;quot;one.php&amp;quot;&amp;gt;one&amp;lt;/A&amp;gt;');&lt;br /&gt;
 '''I'M NOT YELLING!'''&lt;br /&gt;
                '''&amp;lt;a href=&amp;quot;one.php&amp;quot;&amp;gt;one&amp;lt;/a&amp;gt;'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;ucfirst( )&amp;lt;/tt&amp;gt; to capitalize the first character in a string:&lt;br /&gt;
&lt;br /&gt;
 print ucfirst('monkey face');&lt;br /&gt;
 print ucfirst('1 monkey face');&lt;br /&gt;
 '''Monkey face'''&lt;br /&gt;
                '''1 monkey face'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
Note that the second line of output is not &amp;quot;1 Monkey face&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;ucwords( )&amp;lt;/tt&amp;gt; to capitalize the first character of each word in a string:&lt;br /&gt;
&lt;br /&gt;
 print ucwords('1 monkey face');&lt;br /&gt;
 print ucwords(&amp;quot;don't play zone defense against the philadelphia 76-ers&amp;quot;);&lt;br /&gt;
 '''1 Monkey Face'''&lt;br /&gt;
                '''Don't Play Zone Defense Against The Philadelphia 76-ers'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
As expected, &amp;lt;tt&amp;gt;ucwords( )&amp;lt;/tt&amp;gt; doesn't capitalize the &amp;quot;t&amp;quot; in &amp;quot;don't.&amp;quot; But it also doesn't capitalize the &amp;quot;e&amp;quot; in &amp;quot;76-ers.&amp;quot; For &amp;lt;tt&amp;gt;ucwords( )&amp;lt;/tt&amp;gt;, a word is any sequence of nonwhitespace characters that follows one or more whitespace characters. Since both &amp;lt;tt&amp;gt;'&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;-&amp;lt;/tt&amp;gt; aren't whitespace characters, &amp;lt;tt&amp;gt;ucwords( )&amp;lt;/tt&amp;gt; doesn't consider the &amp;quot;t&amp;quot; in &amp;quot;don't&amp;quot; or the &amp;quot;e&amp;quot; in &amp;quot;76-ers&amp;quot; to be word-starting characters.&lt;br /&gt;
&lt;br /&gt;
Both &amp;lt;tt&amp;gt;ucfirst( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;ucwords( )&amp;lt;/tt&amp;gt; don't change the case of nonfirst letters:&lt;br /&gt;
&lt;br /&gt;
 print ucfirst('macWorld says I should get a iBook');&lt;br /&gt;
 print ucwords('eTunaFish.com might buy itunaFish.Com!');&lt;br /&gt;
 '''MacWorld says I should get a iBook'''&lt;br /&gt;
                '''ETunaFish.com Might Buy ItunaFish.Com!'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
The functions &amp;lt;tt&amp;gt;strtolower( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;strtoupper( )&amp;lt;/tt&amp;gt; work on entire strings, not just individual characters. All alphabetic characters are changed to lowercase by &amp;lt;tt&amp;gt;strtolower( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;strtoupper( )&amp;lt;/tt&amp;gt; changes all alphabetic characters to uppercase:&lt;br /&gt;
&lt;br /&gt;
 print strtolower(&amp;quot;I programmed the WOPR and the TRS-80.&amp;quot;);&lt;br /&gt;
 print strtoupper('&amp;quot;since feeling is first&amp;quot; is a poem by e. e. cummings.');&lt;br /&gt;
 '''i programmed the wopr and the trs-80.'''&lt;br /&gt;
                '''&amp;quot;SINCE FEELING IS FIRST&amp;quot; IS A POEM BY E. E. CUMMINGS.'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
When determining upper- and lowercase, these functions respect your locale settings.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
For more information about locale settings, see [[PHP Cookbook/Internationalization and Localization|Chapter 16]]; documentation on &amp;lt;tt&amp;gt;ucfirst( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/ucfirst'', &amp;lt;tt&amp;gt;ucwords( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/ucwords'', &amp;lt;tt&amp;gt;strtolower( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/strtolower'', and &amp;lt;tt&amp;gt;strtoupper( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/strtoupper''.&lt;br /&gt;
&lt;br /&gt;
== Interpolating Functions and Expressions Within Strings ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to include the results of executing a function or expression within a string.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use the string concatenation operator (.) when the value you want to include can't be inside the string:&lt;br /&gt;
&lt;br /&gt;
 print 'You have '.($_REQUEST['boys'] + $_REQUEST['girls']).' children.';&lt;br /&gt;
 print &amp;quot;The word '$word' is &amp;quot;.strlen($word).' characters long.';&lt;br /&gt;
 print 'You owe '.$amounts['payment'].' immediately';&lt;br /&gt;
 print &amp;quot;My circle's diameter is &amp;quot;.$circle-&amp;gt;getDiameter().' inches.';&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
You can put variables, object properties, and array elements (if the subscript is unquoted) directly in double-quoted strings:&lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;I have $children children.&amp;quot;;&lt;br /&gt;
 print &amp;quot;You owe $amounts[payment] immediately.&amp;quot;;&lt;br /&gt;
 print &amp;quot;My circle's diameter is $circle-&amp;gt;diameter inches.&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
Direct interpolation or using string concatenation also works with heredocs. Interpolating with string concatenation in heredocs can look a little strange because the heredoc delimiter and the string concatenation operator have to be on separate lines:&lt;br /&gt;
&lt;br /&gt;
 print &amp;lt;&amp;lt;&amp;lt; END&lt;br /&gt;
 Right now, the time is &lt;br /&gt;
 END&lt;br /&gt;
 . strftime('%c') . &amp;lt;&amp;lt;&amp;lt; END&lt;br /&gt;
  but tomorrow it will be &lt;br /&gt;
 END&lt;br /&gt;
 . strftime('%c',time() + 86400);&lt;br /&gt;
&lt;br /&gt;
Also, if you're interpolating with heredocs, make sure to include appropriate spacing for the whole string to appear properly. In the previous example, &amp;quot;Right now the time&amp;quot; has to include a trailing space, and &amp;quot;but tomorrow it will be&amp;quot; has to include leading and trailing spaces.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
For the syntax to interpolate variable variables (like &amp;lt;tt&amp;gt;${&amp;quot;amount_$i&amp;quot;}&amp;lt;/tt&amp;gt;), see [[PHP Cookbook/Variables#Creating a Dynamic Variable Name|Recipe 5.5]]; documentation on the string concatenation operator at ''http://www.php.net/language.operators.string''.&lt;br /&gt;
&lt;br /&gt;
== Trimming Blanks from a String ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You want to remove whitespace from the beginning or end of a string. For example, you want to clean up user input before validating it.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;ltrim( )&amp;lt;/tt&amp;gt; , &amp;lt;tt&amp;gt;rtrim( )&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;trim( )&amp;lt;/tt&amp;gt;. &amp;lt;tt&amp;gt;ltrim( )&amp;lt;/tt&amp;gt; removes whitespace from the beginning of a string, &amp;lt;tt&amp;gt;rtrim( )&amp;lt;/tt&amp;gt; from the end of a string, and &amp;lt;tt&amp;gt;trim( )&amp;lt;/tt&amp;gt; from both the beginning and end of a string:&lt;br /&gt;
&lt;br /&gt;
 $zipcode = trim($_REQUEST['zipcode']);&lt;br /&gt;
 $no_linefeed = rtrim($_REQUEST['text']);&lt;br /&gt;
 $name = ltrim($_REQUEST['name']);&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
For these functions, whitespace is defined as the following characters: newline, carriage return, space, horizontal and vertical tab, and null.&lt;br /&gt;
&lt;br /&gt;
Trimming whitespace off of strings saves storage space and can make for more precise display of formatted data or text within &amp;lt;tt&amp;gt;&amp;amp;lt;pre&amp;gt;&amp;lt;/tt&amp;gt; tags, for example. If you are doing comparisons with user input, you should trim the data first, so that someone who mistakenly enters &amp;quot;98052 &amp;quot; as their Zip Code isn't forced to fix an error that really isn't. Trimming before exact text comparisons also ensures that, for example, &amp;quot;salami\n&amp;quot; equals &amp;quot;salami.&amp;quot; It's also a good idea to normalize string data by trimming it before storing it in a database.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;trim( )&amp;lt;/tt&amp;gt; functions can also remove user-specified characters from strings. Pass the characters you want to remove as a second argument. You can indicate a range of characters with two dots between the first and last characters in the range.&lt;br /&gt;
&lt;br /&gt;
 // Remove numerals and space from the beginning of the line&lt;br /&gt;
 print ltrim('10 PRINT A$',' 0..9');&lt;br /&gt;
 // Remove semicolon from the end of the line&lt;br /&gt;
 print rtrim('SELECT * FROM turtles;',';');&lt;br /&gt;
 '''PRINT A$'''&lt;br /&gt;
                '''SELECT * FROM turtles'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
PHP also provides &amp;lt;tt&amp;gt;chop( )&amp;lt;/tt&amp;gt; as an alias for &amp;lt;tt&amp;gt;rtrim( )&amp;lt;/tt&amp;gt;. However, you're best off using &amp;lt;tt&amp;gt;rtrim( )&amp;lt;/tt&amp;gt; instead, because PHP's &amp;lt;tt&amp;gt;chop( )&amp;lt;/tt&amp;gt; behaves differently than Perl's &amp;lt;tt&amp;gt;chop( )&amp;lt;/tt&amp;gt; (which is deprecated in favor of &amp;lt;tt&amp;gt;chomp( )&amp;lt;/tt&amp;gt;, anyway) and using it can confuse others when they read your code.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on &amp;lt;tt&amp;gt;trim( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/trim'', &amp;lt;tt&amp;gt;ltrim( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/ltrim'', and &amp;lt;tt&amp;gt;rtrim( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/rtrim''.&lt;br /&gt;
&lt;br /&gt;
== Parsing Comma-Separated Data ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You have data in comma-separated values ( CSV) format, for example a file exported from Excel or a database, and you want to extract the records and fields into a format you can manipulate in PHP.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
If the CSV data is in a file (or available via a URL), open the file with &amp;lt;tt&amp;gt;fopen( )&amp;lt;/tt&amp;gt; and read in the data with &amp;lt;tt&amp;gt;fgetcsv( )&amp;lt;/tt&amp;gt;. This prints out the data in an HTML table:&lt;br /&gt;
&lt;br /&gt;
 $fp = fopen('sample2.csv','r') or die(&amp;quot;can't open file&amp;quot;);&lt;br /&gt;
 print &amp;quot;&amp;amp;lt;table&amp;gt;\n&amp;quot;;&lt;br /&gt;
 while($csv_line = fgetcsv($fp,1024)) {&lt;br /&gt;
     print '&amp;amp;lt;tr&amp;gt;';&lt;br /&gt;
     for ($i = 0, $j = count($csv_line); $i &amp;lt; $j; $i++) {&lt;br /&gt;
         print '&amp;amp;lt;td&amp;gt;'.$csv_line[$i].'&amp;amp;lt;/td&amp;gt;';&lt;br /&gt;
     }&lt;br /&gt;
     print &amp;quot;&amp;amp;lt;/tr&amp;gt;\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 print '&amp;amp;lt;/table&amp;gt;\n';&lt;br /&gt;
 fclose($fp) or die(&amp;quot;can't close file&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
The second argument to &amp;lt;tt&amp;gt;fgetcsv( )&amp;lt;/tt&amp;gt; must be longer than the maximum length of a line in your CSV file. (Don't forget to count the end-of-line whitespace.) If you read in CSV lines longer than 1K, change the 1024 used in this recipe to something that accommodates your line length.&lt;br /&gt;
&lt;br /&gt;
You can pass &amp;lt;tt&amp;gt;fgetcsv( )&amp;lt;/tt&amp;gt; an optional third argument, a delimiter to use instead of a comma (,). Using a different delimiter however, somewhat defeats the purpose of CSV as an easy way to exchange tabular data.&lt;br /&gt;
&lt;br /&gt;
Don't be tempted to bypass &amp;lt;tt&amp;gt;fgetcsv( )&amp;lt;/tt&amp;gt; and just read a line in and &amp;lt;tt&amp;gt;explode( )&amp;lt;/tt&amp;gt; on the commas. CSV is more complicated than that, in order to deal with embedded commas and double quotes. Using &amp;lt;tt&amp;gt;fgetcsv( )&amp;lt;/tt&amp;gt; protects you and your code from subtle errors.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on &amp;lt;tt&amp;gt;fgetcsv( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/fgetcsv''.&lt;br /&gt;
&lt;br /&gt;
== Parsing Fixed-Width Delimited Data ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You need to break apart fixed-width records in strings.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;substr( )&amp;lt;/tt&amp;gt; :&lt;br /&gt;
&lt;br /&gt;
 $fp = fopen('fixed-width-records.txt','r') or die (&amp;quot;can't open file&amp;quot;);&lt;br /&gt;
 while ($s = fgets($fp,1024)) {&lt;br /&gt;
     $fields[1] = substr($s,0,10);  // first field:  first 10 characters of the line&lt;br /&gt;
     $fields[2] = substr($s,10,5);  // second field: next 5 characters of the line&lt;br /&gt;
     $fields[3] = substr($s,15,12); // third field:  next 12 characters of the line&lt;br /&gt;
     // a function to do something with the fields&lt;br /&gt;
     process_fields($fields);&lt;br /&gt;
 }&lt;br /&gt;
 fclose($fp) or die(&amp;quot;can't close file&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Or &amp;lt;tt&amp;gt;unpack( )&amp;lt;/tt&amp;gt; :&lt;br /&gt;
&lt;br /&gt;
 $fp = fopen('fixed-width-records.txt','r') or die (&amp;quot;can't open file&amp;quot;);&lt;br /&gt;
 while ($s = fgets($fp,1024)) {&lt;br /&gt;
     // an associative array with keys &amp;quot;title&amp;quot;, &amp;quot;author&amp;quot;, and &amp;quot;publication_year&amp;quot;&lt;br /&gt;
     $fields = unpack('A25title/A14author/A4publication_year',$s);&lt;br /&gt;
     // a function to do something with the fields&lt;br /&gt;
     process_fields($fields);&lt;br /&gt;
 }&lt;br /&gt;
 fclose($fp) or die(&amp;quot;can't close file&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
Data in which each field is allotted a fixed number of characters per line may look like this list of books, titles, and publication dates:&lt;br /&gt;
&lt;br /&gt;
 $booklist=&amp;lt;&amp;lt;&amp;lt;END&lt;br /&gt;
 Elmer Gantry             Sinclair Lewis1927&lt;br /&gt;
 The Scarlatti InheritanceRobert Ludlum 1971&lt;br /&gt;
 The Parsifal Mosaic      Robert Ludlum 1982&lt;br /&gt;
 Sophie's Choice          William Styron1979&lt;br /&gt;
 END;&lt;br /&gt;
&lt;br /&gt;
In each line, the title occupies the first 25 characters, the author's name the next 14 characters, and the publication year the next 4 characters. Knowing those field widths, it's straightforward to use &amp;lt;tt&amp;gt;substr( )&amp;lt;/tt&amp;gt; to parse the fields into an array:&lt;br /&gt;
&lt;br /&gt;
 $books = explode(&amp;quot;\n&amp;quot;,$booklist);&lt;br /&gt;
 &lt;br /&gt;
 for($i = 0, $j = count($books); $i &amp;lt; $j; $i++) {&lt;br /&gt;
   $book_array[$i]['title'] = substr($books[$i],0,25);&lt;br /&gt;
   $book_array[$i]['author'] = substr($books[$i],25,14);&lt;br /&gt;
   $book_array[$i]['publication_year'] = substr($books[$i],39,4);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Exploding &amp;lt;tt&amp;gt;$booklist&amp;lt;/tt&amp;gt; into an array of lines makes the looping code the same whether it's operating over a string or a series of lines read in from a file.&lt;br /&gt;
&lt;br /&gt;
The loop can be made more flexible by specifying the field names and widths in a separate array that can be passed to a parsing function, as shown in the &amp;lt;tt&amp;gt;pc_fixed_width_substr( )&amp;lt;/tt&amp;gt; function in [[PHP Cookbook/Strings#phpckbk-CHP-1-EX-3|Example 1-3]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;phpckbk-CHP-1-EX-3&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 1-3. pc_fixed_width_substr( )'''&lt;br /&gt;
&lt;br /&gt;
 function pc_fixed_width_substr($fields,$data) {&lt;br /&gt;
   $r = array();&lt;br /&gt;
   for ($i = 0, $j = count($data); $i &amp;lt; $j; $i++) {&lt;br /&gt;
     $line_pos = 0;&lt;br /&gt;
     foreach($fields as $field_name =&amp;gt; $field_length) {&lt;br /&gt;
       $r[$i][$field_name] = rtrim(substr($data[$i],$line_pos,$field_length));&lt;br /&gt;
       $line_pos += $field_length;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
   return $r;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 $book_fields = array('title' =&amp;gt; 25,&lt;br /&gt;
                      'author' =&amp;gt; 14,&lt;br /&gt;
                      'publication_year' =&amp;gt; 4);&lt;br /&gt;
 &lt;br /&gt;
 $book_array = pc_fixed_width_substr($book_fields,$books);&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The variable &amp;lt;tt&amp;gt;$line_pos&amp;lt;/tt&amp;gt; keeps track of the start of each field, and is advanced by the previous field's width as the code moves through each line. Use &amp;lt;tt&amp;gt;rtrim( )&amp;lt;/tt&amp;gt; to remove trailing whitespace from each field.&lt;br /&gt;
&lt;br /&gt;
You can use &amp;lt;tt&amp;gt;unpack( )&amp;lt;/tt&amp;gt; as a substitute for &amp;lt;tt&amp;gt;substr( )&amp;lt;/tt&amp;gt; to extract fields. Instead of specifying the field names and widths as an associative array, create a format string for &amp;lt;tt&amp;gt;unpack( )&amp;lt;/tt&amp;gt;. A fixed-width field extractor using &amp;lt;tt&amp;gt;unpack( )&amp;lt;/tt&amp;gt; looks like the &amp;lt;tt&amp;gt;pc_fixed_width_unpack( )&amp;lt;/tt&amp;gt; function shown in [[PHP Cookbook/Strings#phpckbk-CHP-1-EX-4|Example 1-4]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;phpckbk-CHP-1-EX-4&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 1-4. pc_fixed_width_unpack( )'''&lt;br /&gt;
&lt;br /&gt;
 function pc_fixed_width_unpack($format_string,$data) {&lt;br /&gt;
   $r = array();&lt;br /&gt;
   for ($i = 0, $j = count($data); $i &amp;lt; $j; $i++) {&lt;br /&gt;
     $r[$i] = unpack($format_string,$data[$i]);&lt;br /&gt;
   }&lt;br /&gt;
   return $r;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 $book_array = pc_fixed_width_unpack('A25title/A14author/A4publication_year',&lt;br /&gt;
                                     $books);&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because the &amp;lt;tt&amp;gt;A&amp;lt;/tt&amp;gt; format to &amp;lt;tt&amp;gt;unpack( )&amp;lt;/tt&amp;gt; means &amp;quot;space padded string,&amp;quot; there's no need to &amp;lt;tt&amp;gt;rtrim( )&amp;lt;/tt&amp;gt; off the trailing spaces.&lt;br /&gt;
&lt;br /&gt;
Once the fields have been parsed into &amp;lt;tt&amp;gt;$book_array&amp;lt;/tt&amp;gt; by either function, the data can be printed as an HTML table, for example:&lt;br /&gt;
&lt;br /&gt;
 $book_array = pc_fixed_width_unpack('A25title/A14author/A4publication_year',&lt;br /&gt;
                                     $books);&lt;br /&gt;
 print &amp;quot;&amp;amp;lt;table&amp;gt;\n&amp;quot;;&lt;br /&gt;
 // print a header row&lt;br /&gt;
 print '&amp;amp;lt;tr&amp;gt;&amp;amp;lt;td&amp;gt;';&lt;br /&gt;
 print join('&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;',array_keys($book_array[0]));&lt;br /&gt;
 print &amp;quot;&amp;amp;lt;/td&amp;gt;&amp;amp;lt;/tr&amp;gt;\n&amp;quot;;&lt;br /&gt;
 // print each data row&lt;br /&gt;
 foreach ($book_array as $row) {&lt;br /&gt;
     print '&amp;amp;lt;tr&amp;gt;&amp;amp;lt;td&amp;gt;';&lt;br /&gt;
     print join('&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;',array_values($row));&lt;br /&gt;
     print &amp;quot;&amp;amp;lt;/td&amp;gt;&amp;amp;lt;/tr&amp;gt;\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 print '&amp;amp;lt;/table&amp;gt;\n';&lt;br /&gt;
&lt;br /&gt;
Joining data on &amp;lt;tt&amp;gt;&amp;amp;lt;/td&amp;gt;&amp;amp;lt;td&amp;gt;&amp;lt;/tt&amp;gt; produces a table row that is missing its first &amp;lt;tt&amp;gt;&amp;amp;lt;td&amp;gt;&amp;lt;/tt&amp;gt; and last &amp;lt;tt&amp;gt;&amp;amp;lt;/td&amp;gt;&amp;lt;/tt&amp;gt;. We produce a complete table row by printing out &amp;lt;tt&amp;gt;&amp;amp;lt;tr&amp;gt;&amp;amp;lt;td&amp;gt;&amp;lt;/tt&amp;gt; before the joined data and &amp;lt;tt&amp;gt;&amp;amp;lt;/td&amp;gt;&amp;amp;lt;/tr&amp;gt;&amp;lt;/tt&amp;gt; after the joined data.&lt;br /&gt;
&lt;br /&gt;
Both &amp;lt;tt&amp;gt;substr( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;unpack( )&amp;lt;/tt&amp;gt; have equivalent capabilities when the fixed-width fields are strings, but &amp;lt;tt&amp;gt;unpack( )&amp;lt;/tt&amp;gt; is the better solution when the elements of the fields aren't just strings.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
For more information about &amp;lt;tt&amp;gt;unpack( )&amp;lt;/tt&amp;gt;, see [[PHP Cookbook/Strings#Storing Binary Data in Strings|Recipe 1.14]] and ''http://www.php.net/unpack''; [[PHP Cookbook/Arrays#Turning an Array into a String|Recipe 4.9]] discusses &amp;lt;tt&amp;gt;join( )&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Taking Strings Apart ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You need to break a string into pieces. For example, you want to access each line that a user enters in a &amp;lt;tt&amp;gt;&amp;lt;textarea&amp;gt;&amp;lt;/tt&amp;gt; form field.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;explode( )&amp;lt;/tt&amp;gt; if what separates the pieces is a constant string:&lt;br /&gt;
&lt;br /&gt;
 $words = explode(' ','My sentence is not very complicated');&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;split( )&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;preg_split( )&amp;lt;/tt&amp;gt; if you need a POSIX or Perl regular expression to describe the separator:&lt;br /&gt;
&lt;br /&gt;
 $words = split(' +','This sentence  has  some extra whitespace  in it.');&lt;br /&gt;
 $words = preg_split('/\d\. /','my day: 1. get up 2. get dressed 3. eat toast');&lt;br /&gt;
 $lines = preg_split('/[\n\r]+/',$_REQUEST['textarea']);&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;spliti( )&amp;lt;/tt&amp;gt; or the &amp;lt;tt&amp;gt;/i&amp;lt;/tt&amp;gt; flag to &amp;lt;tt&amp;gt;preg_split( )&amp;lt;/tt&amp;gt; for case-insensitive separator matching:&lt;br /&gt;
&lt;br /&gt;
 $words = spliti(' x ','31 inches x 22 inches X 9 inches');&lt;br /&gt;
 $words = preg_split('/ x /i','31 inches x 22 inches X 9 inches');&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
The simplest solution of the bunch is &amp;lt;tt&amp;gt;explode( )&amp;lt;/tt&amp;gt;. Pass it your separator string, the string to be separated, and an optional limit on how many elements should be returned:&lt;br /&gt;
&lt;br /&gt;
 $dwarves = 'dopey,sleepy,happy,grumpy,sneezy,bashful,doc';&lt;br /&gt;
 $dwarf_array = explode(',',$dwarves);&lt;br /&gt;
&lt;br /&gt;
Now &amp;lt;tt&amp;gt;$dwarf_array&amp;lt;/tt&amp;gt; is a seven element array:&lt;br /&gt;
&lt;br /&gt;
 print_r($dwarf_array);&lt;br /&gt;
 '''Array'''&lt;br /&gt;
                '''('''&lt;br /&gt;
                '''    [0] =&amp;gt; dopey'''&lt;br /&gt;
                '''    [1] =&amp;gt; sleepy'''&lt;br /&gt;
                '''    [2] =&amp;gt; happy'''&lt;br /&gt;
                '''    [3] =&amp;gt; grumpy'''&lt;br /&gt;
                '''    [4] =&amp;gt; sneezy'''&lt;br /&gt;
                '''    [5] =&amp;gt; bashful'''&lt;br /&gt;
                '''    [6] =&amp;gt; doc'''&lt;br /&gt;
                ''')'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
If the specified limit is less than the number of possible chunks, the last chunk contains the remainder:&lt;br /&gt;
&lt;br /&gt;
 $dwarf_array = explode(',',$dwarves,5);&lt;br /&gt;
 print_r($dwarf_array);&lt;br /&gt;
 '''Array'''&lt;br /&gt;
                '''('''&lt;br /&gt;
                '''    [0] =&amp;gt; dopey'''&lt;br /&gt;
                '''    [1] =&amp;gt; sleepy'''&lt;br /&gt;
                '''    [2] =&amp;gt; happy'''&lt;br /&gt;
                '''    [3] =&amp;gt; grumpy'''&lt;br /&gt;
                '''    [4] =&amp;gt; sneezy,bashful,doc'''&lt;br /&gt;
                ''')'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
The separator is treated literally by &amp;lt;tt&amp;gt;explode( )&amp;lt;/tt&amp;gt;. 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.&lt;br /&gt;
&lt;br /&gt;
With &amp;lt;tt&amp;gt;split( )&amp;lt;/tt&amp;gt;, you have more flexibility. Instead of a string literal as a separator, it uses a POSIX regular expression:&lt;br /&gt;
&lt;br /&gt;
 $more_dwarves = 'cheeky,fatso, wonder boy, chunky,growly, groggy, winky';&lt;br /&gt;
 $more_dwarf_array = split(', ?',$more_dwarves);&lt;br /&gt;
&lt;br /&gt;
This regular expression splits on a comma followed by an optional space, which treats all the new dwarves properly. Those with a space in their name aren't broken up, but everyone is broken apart whether they are separated by &amp;quot;,&amp;quot; or &amp;quot;, &amp;quot;:&lt;br /&gt;
&lt;br /&gt;
 print_r($more_dwarf_array);&lt;br /&gt;
 '''Array'''&lt;br /&gt;
                '''('''&lt;br /&gt;
                '''    [0] =&amp;gt; cheeky'''&lt;br /&gt;
                '''    [1] =&amp;gt; fatso'''&lt;br /&gt;
                '''    [2] =&amp;gt; wonder boy'''&lt;br /&gt;
                '''    [3] =&amp;gt; chunky'''&lt;br /&gt;
                '''    [4] =&amp;gt; growly'''&lt;br /&gt;
                '''    [5] =&amp;gt; groggy'''&lt;br /&gt;
                '''    [6] =&amp;gt; winky'''&lt;br /&gt;
                ''')'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
Similar to &amp;lt;tt&amp;gt;split( )&amp;lt;/tt&amp;gt; is &amp;lt;tt&amp;gt;preg_split( )&amp;lt;/tt&amp;gt; , which uses a Perl-compatible regular-expression engine instead of a POSIX regular-expression engine. With &amp;lt;tt&amp;gt;preg_split( )&amp;lt;/tt&amp;gt;, you can take advantage of various Perlish regular-expression extensions, as well as tricks such as including the separator text in the returned array of strings:&lt;br /&gt;
&lt;br /&gt;
 $math = &amp;quot;3 + 2 / 7 - 9&amp;quot;;&lt;br /&gt;
 $stack = preg_split('/ *([+\-\/*]) */',$math,-1,PREG_SPLIT_DELIM_CAPTURE);&lt;br /&gt;
 print_r($stack);&lt;br /&gt;
 '''Array'''&lt;br /&gt;
                '''('''&lt;br /&gt;
                '''    [0] =&amp;gt; 3'''&lt;br /&gt;
                '''    [1] =&amp;gt; +'''&lt;br /&gt;
                '''    [2] =&amp;gt; 2'''&lt;br /&gt;
                '''    [3] =&amp;gt; /'''&lt;br /&gt;
                '''    [4] =&amp;gt; 7'''&lt;br /&gt;
                '''    [5] =&amp;gt; -'''&lt;br /&gt;
                '''    [6] =&amp;gt; 9'''&lt;br /&gt;
                ''')'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
The separator regular expression looks for the four mathematical operators (&amp;lt;tt&amp;gt;+&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;-&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;/&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;*&amp;lt;/tt&amp;gt;), surrounded by optional leading or trailing spaces. The &amp;lt;tt&amp;gt;PREG_SPLIT_DELIM_CAPTURE&amp;lt;/tt&amp;gt; flag tells &amp;lt;tt&amp;gt;preg_split( )&amp;lt;/tt&amp;gt; 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't have any spaces in it.&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Regular expressions are discussed in more detail in [[PHP Cookbook/Regular Expressions|Chapter 13]]; documentation on &amp;lt;tt&amp;gt;explode( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/explode'', &amp;lt;tt&amp;gt;split( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/split'', and &amp;lt;tt&amp;gt;preg_split( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/preg-split''.&lt;br /&gt;
&lt;br /&gt;
== Wrapping Text at a Certain Line Length ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
You need to wrap lines in a string. For example, you want to display text in &amp;lt;tt&amp;gt;&amp;amp;lt;pre&amp;gt;&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;&amp;amp;lt;/pre&amp;gt;&amp;lt;/tt&amp;gt; tags but have it stay within a regularly sized browser window.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;wordwrap( )&amp;lt;/tt&amp;gt; :&lt;br /&gt;
&lt;br /&gt;
 $s = &amp;quot;Four score and seven years ago our fathers brought forth on this continen&lt;br /&gt;
 t a new nation, conceived in liberty and dedicated to the proposition that all &lt;br /&gt;
 men are created equal.&amp;quot;;&lt;br /&gt;
 &lt;br /&gt;
 print &amp;quot;&amp;amp;lt;pre&amp;gt;\n&amp;quot;.wordwrap($s).&amp;quot;\n&amp;amp;lt;/pre&amp;gt;&amp;quot;;&lt;br /&gt;
 '''&amp;amp;lt;pre&amp;gt;'''&lt;br /&gt;
                '''Four score and seven years ago our fathers brought forth on this continent'''&lt;br /&gt;
                '''a new nation, conceived in liberty and dedicated to the proposition that'''&lt;br /&gt;
                '''all men are created equal.'''&lt;br /&gt;
                '''&amp;amp;lt;/pre&amp;gt;'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
By default, &amp;lt;tt&amp;gt;wordwrap( )&amp;lt;/tt&amp;gt; wraps text at 75 characters per line. An optional second argument specifies different line length:&lt;br /&gt;
&lt;br /&gt;
 print wordwrap($s,50);&lt;br /&gt;
 '''Four score and seven years ago our fathers brought'''&lt;br /&gt;
                '''forth on this continent a new nation, conceived in'''&lt;br /&gt;
                '''liberty and dedicated to the proposition that all'''&lt;br /&gt;
                '''men are created equal.'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
Other characters besides &amp;quot;\n&amp;quot; can be used for linebreaks. For double spacing, use &amp;quot;\n\n&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
 print wordwrap($s,50,&amp;quot;\n\n&amp;quot;);&lt;br /&gt;
 '''Four score and seven years ago our fathers brought'''&lt;br /&gt;
 &lt;br /&gt;
                '''forth on this continent a new nation, conceived in'''&lt;br /&gt;
 &lt;br /&gt;
                '''liberty and dedicated to the proposition that all'''&lt;br /&gt;
 &lt;br /&gt;
                '''men are created equal.'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
There is an optional fourth argument to &amp;lt;tt&amp;gt;wordwrap( )&amp;lt;/tt&amp;gt; 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:&lt;br /&gt;
&lt;br /&gt;
 print wordwrap('jabberwocky',5);&lt;br /&gt;
 print wordwrap('jabberwocky',5,&amp;quot;\n&amp;quot;,1);&lt;br /&gt;
 '''jabberwocky'''&lt;br /&gt;
 &lt;br /&gt;
                '''jabbe'''&lt;br /&gt;
                '''rwock'''&lt;br /&gt;
                '''y'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on &amp;lt;tt&amp;gt;wordwrap( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/wordwrap''.&lt;br /&gt;
&lt;br /&gt;
== Storing Binary Data in Strings ==&lt;br /&gt;
&lt;br /&gt;
=== Problem ===&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Solution ===&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;pack( )&amp;lt;/tt&amp;gt; to store binary data in a string:&lt;br /&gt;
&lt;br /&gt;
 $packed = pack('S4',1974,106,28225,32725);&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;tt&amp;gt;unpack( )&amp;lt;/tt&amp;gt; to extract binary data from a string:&lt;br /&gt;
&lt;br /&gt;
 $nums = unpack('S4',$packed);&lt;br /&gt;
&lt;br /&gt;
=== Discussion ===&lt;br /&gt;
&lt;br /&gt;
The first argument to &amp;lt;tt&amp;gt;pack( )&amp;lt;/tt&amp;gt; is a format string that describes how to encode the data that's passed in the rest of the arguments. The format string &amp;lt;tt&amp;gt;S4&amp;lt;/tt&amp;gt; tells &amp;lt;tt&amp;gt;pack( )&amp;lt;/tt&amp;gt; to produce four unsigned short 16-bit numbers in machine byte order from its input data. Given 1974, 106, 28225, and 32725 as input, 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.&lt;br /&gt;
&lt;br /&gt;
The first argument to &amp;lt;tt&amp;gt;unpack( )&amp;lt;/tt&amp;gt; is also a format string, and the second argument is the data to decode. Passing a format string of &amp;lt;tt&amp;gt;S4&amp;lt;/tt&amp;gt;, the eight-byte sequence that &amp;lt;tt&amp;gt;pack( )&amp;lt;/tt&amp;gt; produced returns a four-element array of the original numbers:&lt;br /&gt;
&lt;br /&gt;
 print_r($nums);&lt;br /&gt;
 '''Array'''&lt;br /&gt;
                '''('''&lt;br /&gt;
                '''    [1] =&amp;gt; 1974'''&lt;br /&gt;
                '''    [2] =&amp;gt; 106'''&lt;br /&gt;
                '''    [3] =&amp;gt; 28225'''&lt;br /&gt;
                '''    [4] =&amp;gt; 32725'''&lt;br /&gt;
                ''')'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
In &amp;lt;tt&amp;gt;unpack( )&amp;lt;/tt&amp;gt;, format characters and their count can be followed by a string to be used as an array key. For example:&lt;br /&gt;
&lt;br /&gt;
 $nums = unpack('S4num',$packed);&lt;br /&gt;
 print_r($nums);&lt;br /&gt;
 '''Array'''&lt;br /&gt;
                '''('''&lt;br /&gt;
                '''    [num1] =&amp;gt; 1974'''&lt;br /&gt;
                '''    [num2] =&amp;gt; 106'''&lt;br /&gt;
                '''    [num3] =&amp;gt; 28225'''&lt;br /&gt;
                '''    [num4] =&amp;gt; 32725'''&lt;br /&gt;
                ''')'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
Multiple format characters must be separated with &amp;lt;tt&amp;gt;/&amp;lt;/tt&amp;gt; in &amp;lt;tt&amp;gt;unpack( )&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 $nums = unpack('S1a/S1b/S1c/S1d',$packed);&lt;br /&gt;
 print_r($nums);&lt;br /&gt;
 '''Array'''&lt;br /&gt;
                '''('''&lt;br /&gt;
                '''    [a] =&amp;gt; 1974'''&lt;br /&gt;
                '''    [b] =&amp;gt; 106'''&lt;br /&gt;
                '''    [c] =&amp;gt; 28225'''&lt;br /&gt;
                '''    [d] =&amp;gt; 32725'''&lt;br /&gt;
                ''')'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
The format characters that can be used with &amp;lt;tt&amp;gt;pack( )&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;unpack( )&amp;lt;/tt&amp;gt; are listed in [[PHP Cookbook/Strings#phpckbk-CHP-1-TABLE-2|Table 1-2]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;phpckbk-CHP-1-TABLE-2&amp;quot;&amp;gt;&lt;br /&gt;
'''Table 1-2. Format characters for pack( ) and unpack( )'''&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; &lt;br /&gt;
|-&lt;br /&gt;
! Format character !! Data type&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt; || NUL-padded string&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;A&amp;lt;/tt&amp;gt; || Space-padded string&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;h&amp;lt;/tt&amp;gt; || Hex string, low nibble first&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;H&amp;lt;/tt&amp;gt; || Hex string, high nibble first&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;c&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;signed char&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;C&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;unsigned char&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;s&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;signed short&amp;lt;/tt&amp;gt; (16 bit, machine byte order)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;S&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;unsigned short&amp;lt;/tt&amp;gt; (16 bit, machine byte order)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;n&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;unsigned short&amp;lt;/tt&amp;gt; (16 bit, big endian byte order)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;v&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;unsigned short&amp;lt;/tt&amp;gt; (16 bit, little endian byte order)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;i&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;signed int&amp;lt;/tt&amp;gt; (machine-dependent size and byte order)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;I&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;unsigned int&amp;lt;/tt&amp;gt; (machine-dependent size and byte order)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;l&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;signed long&amp;lt;/tt&amp;gt; (32 bit, machine byte order)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;L&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;unsigned long&amp;lt;/tt&amp;gt; (32 bit, machine byte order)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;N&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;unsigned long&amp;lt;/tt&amp;gt; (32 bit, big endian byte order)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;V&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;unsigned long&amp;lt;/tt&amp;gt; (32 bit, little endian byte order)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;f&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;float&amp;lt;/tt&amp;gt; (machine dependent size and representation)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;d&amp;lt;/tt&amp;gt; || &amp;lt;tt&amp;gt;double&amp;lt;/tt&amp;gt; (machine dependent size and representation)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;x&amp;lt;/tt&amp;gt; || NUL byte&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;X&amp;lt;/tt&amp;gt; || Back up one byte&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;@&amp;lt;/tt&amp;gt; || NUL-fill to absolute position&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;A&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;h&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;H&amp;lt;/tt&amp;gt;, a number after the format character indicates how long the string is. For example, &amp;lt;tt&amp;gt;A25&amp;lt;/tt&amp;gt; means a 25-character space-padded string. For other format characters, a following number means how many of that type appear consecutively in a string. Use &amp;lt;tt&amp;gt;*&amp;lt;/tt&amp;gt; to take the rest of the available data.&lt;br /&gt;
&lt;br /&gt;
You can convert between data types with &amp;lt;tt&amp;gt;unpack( )&amp;lt;/tt&amp;gt;. This example fills the array &amp;lt;tt&amp;gt;$ascii&amp;lt;/tt&amp;gt; with the ASCII values of each character in &amp;lt;tt&amp;gt;$s&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 $s = 'platypus';&lt;br /&gt;
 $ascii = unpack('c*',$s);&lt;br /&gt;
 print_r($ascii);&lt;br /&gt;
 '''Array'''&lt;br /&gt;
                '''('''&lt;br /&gt;
                '''    [1] =&amp;gt; 112'''&lt;br /&gt;
                '''    [2] =&amp;gt; 108'''&lt;br /&gt;
                '''    [3] =&amp;gt; 97'''&lt;br /&gt;
                '''    [4] =&amp;gt; 116'''&lt;br /&gt;
                '''    [5] =&amp;gt; 121'''&lt;br /&gt;
                '''    [6] =&amp;gt; 112'''&lt;br /&gt;
                '''    [7] =&amp;gt; 117'''&lt;br /&gt;
                '''    [8] =&amp;gt; 115'''&lt;br /&gt;
                ''')'''&lt;br /&gt;
             &lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
&lt;br /&gt;
Documentation on &amp;lt;tt&amp;gt;pack( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/pack'' and &amp;lt;tt&amp;gt;unpack( )&amp;lt;/tt&amp;gt; at ''http://www.php.net/unpack'' .&lt;/div&gt;</description>
			<pubDate>Thu, 06 Mar 2008 22:28:45 GMT</pubDate>			<dc:creator>Docbook2Wiki</dc:creator>			<comments>http://commons.oreilly.com/wiki/index.php/Talk:PHP_Cookbook/Strings</comments>		</item>
	</channel>
</rss>