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

<channel>
	<title>PHP and Web Development Blog &#187; string</title>
	<atom:link href="http://www.ebrueggeman.com/blog/tag/string/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ebrueggeman.com/blog</link>
	<description>Tips and Tricks for Web Developers</description>
	<lastBuildDate>Tue, 03 Aug 2010 17:21:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Easily Parse a String</title>
		<link>http://www.ebrueggeman.com/blog/easily-parse-a-string/</link>
		<comments>http://www.ebrueggeman.com/blog/easily-parse-a-string/#comments</comments>
		<pubDate>Mon, 27 Oct 2008 06:46:55 +0000</pubDate>
		<dc:creator>Elliott Brueggeman</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[parse]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://www.ebrueggeman.com/blog/?p=43</guid>
		<description><![CDATA[I recently came across the need to efficiently parse a string and find what was in between two &#8220;marker&#8221; strings. I was parsing a logfile with a particular preset format. The line looked like this:

$str = '2008-10-07_00:00:19 - ip:[213.21.198.231] page:[/phpsimplechat/documentation_usage.php]';

I wanted to be able to easily pull out the ip and page information in a [...]]]></description>
			<content:encoded><![CDATA[<p>I recently came across the need to efficiently parse a string and find what was in between two &#8220;marker&#8221; strings. I was parsing a logfile with a particular preset format. The line looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="">'2008-10-07_00:00:19 - ip:[213.21.198.231] page:[/phpsimplechat/documentation_usage.php]'</span>;</pre></div></div>

<p>I wanted to be able to easily pull out the ip and page information in a straight-forward way without repeating code. After a little tinkering I came up with a get_token() function that accomplished my goal nicely. The function is below:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_token<span style="color: #009900;">&#40;</span><span style="color: #000088;">$start_token</span><span style="color: #339933;">,</span> <span style="color: #000088;">$end_token</span><span style="color: #339933;">,</span> <span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span> <span style="color: #000088;">$offset</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> stripos<span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span> <span style="color: #000088;">$start_token</span><span style="color: #339933;">,</span> <span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #000088;">$end</span> <span style="color: #339933;">=</span> stripos<span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span> <span style="color: #000088;">$start</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$end_token</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$start</span> <span style="color: #339933;">!==</span> <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$end</span> <span style="color: #339933;">!==</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$start_pos</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$start</span> <span style="color: #339933;">+</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$start_token</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #000088;">$end_pos</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$end</span> <span style="color: #339933;">-</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$end_token</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span> <span style="color: #000088;">$start_pos</span><span style="color: #339933;">,</span> <span style="color: #000088;">$end_pos</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Let&#8217;s take my above example &#8211;  if I want to pull out the ip and page information from the given string, I only need to implement the below code:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="">'2008-10-07_00:00:19 - ip:[213.21.198.231] page:[/phpsimplechat/documentation_usage.php]'</span>;
<span style="color: #000088;">$ip</span> <span style="color: #339933;">=</span> get_token<span style="color: #009900;">&#40;</span><span style="">'ip:['</span><span style="color: #339933;">,</span> <span style="">']'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> get_token<span style="color: #009900;">&#40;</span><span style="">'page:['</span><span style="color: #339933;">,</span> <span style="">']'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>The optional offset argument allows you to specify how many characters into the $haystack that you want to start looking;</p>
<p>Here is another example usage in which we want to pull the title out of some HTML code.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="">'This is the body'</span>;
<span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> get_token<span style="color: #009900;">&#40;</span><span style="">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.ebrueggeman.com/blog/easily-parse-a-string/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
