<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: PHP &#8211; parse a string between two strings</title>
	<atom:link href="http://www.justin-cook.com/wp/2006/03/31/php-parse-a-string-between-two-strings/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.justin-cook.com/wp/2006/03/31/php-parse-a-string-between-two-strings/</link>
	<description>A web, games, technology, programming and SEO blog by Justin Cook</description>
	<lastBuildDate>Wed, 04 Jan 2012 07:53:46 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Erik</title>
		<link>http://www.justin-cook.com/wp/2006/03/31/php-parse-a-string-between-two-strings/comment-page-2/#comment-157594</link>
		<dc:creator>Erik</dc:creator>
		<pubDate>Sat, 03 Dec 2011 14:49:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.justin-cook.com/wp/2006/03/31/php-parse-a-string-between-two-strings/#comment-157594</guid>
		<description>I was writing a php based search engine script for my site and wanted to:
1. fetch the content of a page
2. find an occurence of the searched string
3. store the url and some text around the found string
4. get all links on that page and
5. loop throught these links and repeat steps 1-4.


I wont write the whole script, but only the extended default function that Justin has put up. So here it is:

- added a while loop which cycles throught the whole string (could be a whole html page)
- added an limit var to control how many occurences I want to fetch
- added an extra parameter $distinct which enables me to NOT store strings that allready have been stored in the results array - nice for duplicate URLs

NOTE: I am sure this function could be improved so dont hesitate do to so :)

To test it copy this code:


function get_string_between($string, $start, $end, $limit, $distinct=true)
{	
	$result=array();
	$num_of_occurences=0;
	while(($ini = strpos($string,$start, $new_start))!==FALSE)
	{
		$ini += strlen($start);
		$new_start = strpos($string,$end,$ini);
		$len=$new_start - $ini;
		$new_start+= strlen($end);
		$found_string=substr($string,$ini,$len);
		
		if($distinct)
		{
			if(!in_array($found_string, $result))
			{
				$result[]=$found_string;
				$num_of_occurences++;
			}
		}
		else
		{
			$result[]=$found_string;
			$num_of_occurences++;
		}
		
		if($limit==$num_of_occurences &amp;&amp; $limit!=0)
			break;
	}
	return $result;
}



$content = file_get_contents(&#039;http://www.selectif.si/&#039;);

$result = get_string_between($content, &quot;href=\&quot;&quot;, &quot;\&quot;&quot;, 0, true);


echo &quot;&quot;;
print_r($result);
echo &quot;&quot;;</description>
		<content:encoded><![CDATA[<p>I was writing a php based search engine script for my site and wanted to:<br />
1. fetch the content of a page<br />
2. find an occurence of the searched string<br />
3. store the url and some text around the found string<br />
4. get all links on that page and<br />
5. loop throught these links and repeat steps 1-4.</p>
<p>I wont write the whole script, but only the extended default function that Justin has put up. So here it is:</p>
<p>- added a while loop which cycles throught the whole string (could be a whole html page)<br />
- added an limit var to control how many occurences I want to fetch<br />
- added an extra parameter $distinct which enables me to NOT store strings that allready have been stored in the results array &#8211; nice for duplicate URLs</p>
<p>NOTE: I am sure this function could be improved so dont hesitate do to so <img src='http://www.justin-cook.com/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>To test it copy this code:</p>
<p>function get_string_between($string, $start, $end, $limit, $distinct=true)<br />
{<br />
	$result=array();<br />
	$num_of_occurences=0;<br />
	while(($ini = strpos($string,$start, $new_start))!==FALSE)<br />
	{<br />
		$ini += strlen($start);<br />
		$new_start = strpos($string,$end,$ini);<br />
		$len=$new_start &#8211; $ini;<br />
		$new_start+= strlen($end);<br />
		$found_string=substr($string,$ini,$len);</p>
<p>		if($distinct)<br />
		{<br />
			if(!in_array($found_string, $result))<br />
			{<br />
				$result[]=$found_string;<br />
				$num_of_occurences++;<br />
			}<br />
		}<br />
		else<br />
		{<br />
			$result[]=$found_string;<br />
			$num_of_occurences++;<br />
		}</p>
<p>		if($limit==$num_of_occurences &amp;&amp; $limit!=0)<br />
			break;<br />
	}<br />
	return $result;<br />
}</p>
<p>$content = file_get_contents(&#039;<a href="http://www.selectif.si/" rel="nofollow">http://www.selectif.si/</a>&#039;);</p>
<p>$result = get_string_between($content, &#034;href=\&#034;", &#034;\&#034;", 0, true);</p>
<p>echo &#034;&#034;;<br />
print_r($result);<br />
echo &#034;&#034;;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: darthmartin</title>
		<link>http://www.justin-cook.com/wp/2006/03/31/php-parse-a-string-between-two-strings/comment-page-2/#comment-157551</link>
		<dc:creator>darthmartin</dc:creator>
		<pubDate>Fri, 07 Oct 2011 06:23:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.justin-cook.com/wp/2006/03/31/php-parse-a-string-between-two-strings/#comment-157551</guid>
		<description>You rock!! Thank you, thank u, thanku!!</description>
		<content:encoded><![CDATA[<p>You rock!! Thank you, thank u, thanku!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simply Super BBBBBBBB</title>
		<link>http://www.justin-cook.com/wp/2006/03/31/php-parse-a-string-between-two-strings/comment-page-2/#comment-157505</link>
		<dc:creator>Simply Super BBBBBBBB</dc:creator>
		<pubDate>Thu, 08 Sep 2011 10:14:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.justin-cook.com/wp/2006/03/31/php-parse-a-string-between-two-strings/#comment-157505</guid>
		<description>Simply Super BBBBBBBB</description>
		<content:encoded><![CDATA[<p>Simply Super BBBBBBBB</p>
]]></content:encoded>
	</item>
</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
