<?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>code-diesel &#187; search</title>
	<atom:link href="http://www.codediesel.com/tag/search/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>WolframAlpha &#8211; Dawn of a new search era</title>
		<link>http://www.codediesel.com/tools/wolframalpha-dawn-of-a-new-search/</link>
		<comments>http://www.codediesel.com/tools/wolframalpha-dawn-of-a-new-search/#comments</comments>
		<pubDate>Fri, 15 May 2009 06:28:20 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[tools]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=901</guid>
		<description><![CDATA[wolfram alpha engine]]></description>
			<content:encoded><![CDATA[<p>Today will be the launch of <a target="_blank" href="http://www.wolframalpha.com/index.html">WolframAlpha</a>, the new knowledge engine everyone is waiting for. WolframAlpha will truly revolutionize information search in that the information returned will be computed for your particular purpose from a gargantuan knowledge base. It rightly shows the power of intelligent computing. Even though I&#8217;m a developer myself and regularly keep myself abreast in the software field, I couldn&#8217;t have imagined the amount of advances made in natural language processing, heuristics and knowledge representation.<br />
<span id="more-901"></span></p>
<p>According to the sites faq:</p>
<p><em>Wolfram|Alpha&#8217;s long-term goal is to make all systematic knowledge immediately computable and accessible to everyone. We aim to collect and curate all objective data; implement every known model, method, and algorithm; and make it possible to compute whatever can be computed about anything. Our goal is to build on the achievements of science and other systematization of knowledge to provide a single source that can be relied on by everyone for definitive answers to factual queries.</em></p>
<p>Conceived by the brilliant <a target="_blank" href="http://en.wikipedia.org/wiki/Stephen_Wolfram">Stephen Wolfram</a>, its another feather in his cap after his magnificent <a target="_blank" href="http://www.wolfram.com/products/mathematica/index.html">Mathematica</a> software.</p>
<p>As an example if you search for &#8220;life expectancy male age 40 Finland&#8221; the engine will return the following:<br />
<a href="http://www.codediesel.com/wp-content/uploads/2009/05/wolframalpha_example1.jpg"><img src="http://www.codediesel.com/wp-content/uploads/2009/05/wolframalpha_example1-448x1024.jpg" alt="wolframalpha_example1" title="wolframalpha_example1" width="448" height="1024" class="aligncenter size-large wp-image-905" /></a></p>
<p>Or if you search for &#8220;president of Brazil in 1922&#8243;:</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2009/05/wolframalpha_example2.jpg"><img src="http://www.codediesel.com/wp-content/uploads/2009/05/wolframalpha_example2.jpg" alt="wolframalpha_example2" title="wolframalpha_example2" width="448" height="407" class="aligncenter size-full wp-image-907" /></a></p>
<p>Or a mathematical query like &#8220;3,7,15,31,63,&#8230;&#8221;, and wolframalpha computes the next terms in the series and also gives the functions for the same:</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2009/05/wolframalpha_example3.jpg"><img src="http://www.codediesel.com/wp-content/uploads/2009/05/wolframalpha_example3.jpg" alt="wolframalpha_example3" title="wolframalpha_example3" width="448" height="585" class="aligncenter size-full wp-image-911" /></a></p>
<p>The other interesting feature of the software engine is that for many queries it computes the information from realtime data. For example if you were to search for &#8216;moon&#8217; than wolframalpha would give all the details of the same and also such information as the present position in the sky, distance from the earth, etc. You can watch the detailed examples <a target="_blank" href="http://www.wolframalpha.com/screencast/introducingwolframalpha.html">here</a>. Imagine what this kind of information can do in the hands of researchers and scientists. An exponential increase in the present knowledge in all areas of science.</p>
<p>The above example are just the tip of what the engine can do. Imagine a robot linked to the WolframAlpha engine and you can get the picture of where we are heading. We are on our way to building a true <a target="_blank" href="http://en.wikipedia.org/wiki/Encyclopedia_Galactica">Encyclopedia Galactica</a>. Knowledge search will never be the same again. Wish Asimov was here!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/tools/wolframalpha-dawn-of-a-new-search/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Search &amp; replace in files using php</title>
		<link>http://www.codediesel.com/php/search-replace-in-files-using-php/</link>
		<comments>http://www.codediesel.com/php/search-replace-in-files-using-php/#comments</comments>
		<pubDate>Mon, 11 May 2009 13:57:51 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[files]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=819</guid>
		<description><![CDATA[Searching and replacing content in files is a common task all of us do regularly. Most programmers will implement it using Perl a shell script or through a editor. Perl offers itself as an excellent tool for the required purpose; we PHP programmers are not quite so lucky in that matter. Search/replace is easier from [...]]]></description>
			<content:encoded><![CDATA[<p>Searching and replacing content in files is a common task all of us do regularly. Most programmers will implement it using Perl a shell script or through a editor. Perl offers itself as an excellent tool for the required purpose; we PHP programmers are not quite so lucky in that matter. Search/replace is easier from a shell prompt or an editor, but what if you have to do the same programatically in php. <a target="_blank" href="http://pear.php.net/package/File_SearchReplace/download">File_SearchReplace</a> is a pear package that helps you search/replace in files through a nice object oriented interface.<br />
<span id="more-819"></span><br />
<strong>Installation</strong><br />
Pear installation as usual is simple.</p>

<div class="wp_codebox"><table><tr id="p8197"><td class="code" id="p819code7"><pre class="dos" style="font-family:monospace;">c:/&gt; pear install File_SearchReplace</pre></td></tr></table></div>

<p><strong>Doing a simple search &#038; replace</strong><br />
The following is an simple example code that searches the file &#8216;fruits.txt&#8217; and replaces all occurrences of &#8216;apples&#8217; with &#8216;oranges&#8217;. The <em>getNumOccurences</em> function returns the total number of replaced strings in the file.</p>

<div class="wp_codebox"><table><tr id="p8198"><td class="code" id="p819code8"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span> <span style="color: #0000ff;">'File/SearchReplace.php'</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$files_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fruits.txt&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #000088;">$search_string</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;apples&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$replace_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;oranges&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$snr</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> File_SearchReplace<span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$replace_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$files_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #009933; font-style: italic;">// directorie(s) to search</span>
                              <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doSearch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;The number of replaces done : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNumOccurences</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The fourth option in the <em>File_SearchReplace</em> specifies a optional directory name to search. If the directory name is empty than the files will be searched in the current directory or in the respective path if a path is also included with the filename. Following is an example if you want to search all files in the directory &#8216;nature/fruits&#8217;.</p>

<div class="wp_codebox"><table><tr id="p8199"><td class="code" id="p819code9"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span> <span style="color: #0000ff;">'File/SearchReplace.php'</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$files_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$direc_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'nature/fruits/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$search_string</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;apples&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$replace_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;oranges&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$snr</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> File_SearchReplace<span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$replace_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$files_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$direc_to_search</span><span style="color: #339933;">,</span> <span style="color: #009933; font-style: italic;">// directory to search</span>
                              <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// 'true' to search subdirectories</span>
&nbsp;
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doSearch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;The number of replaces done : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNumOccurences</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>All the four starting options of <em>File_SearchReplace</em> are of mixed type; i.e they take a string or an array of strings as their options. For example in the following all occurrences of &#8216;apples&#8217; will be replaced by &#8216;oranges&#8217; and that of &#8216;pears&#8217; by &#8216;grapes&#8217;.</p>

<div class="wp_codebox"><table><tr id="p81910"><td class="code" id="p819code10"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span> <span style="color: #0000ff;">'File/SearchReplace.php'</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$files_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$direc_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'nature/fruits/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$search_string</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'apples'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pears'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$replace_string</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'oranges'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'grapes'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$snr</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> File_SearchReplace<span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$replace_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$files_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$direc_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doSearch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;The number of replaces done : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNumOccurences</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>You don&#8217;t have to create a new instance everytime you need a new search, you can set the various parameters through the interface provided by the <em>File_SearchReplace</em> class as shown below.</p>

<div class="wp_codebox"><table><tr id="p81911"><td class="code" id="p819code11"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span> <span style="color: #0000ff;">'File/SearchReplace.php'</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$files_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$direc_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'nature/fruits/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$search_string</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'apples'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pears'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$replace_string</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'oranges'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'grapes'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$snr</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> File_SearchReplace<span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$replace_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$files_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$direc_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doSearch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;The number of replaces done : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNumOccurences</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Start a new search */</span>
&nbsp;
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFind</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;oranges&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setReplace</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;berries&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doSearch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;The number of replaces done : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNumOccurences</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Regular Expression search</strong><br />
You can also use a regular expression in a search string, but before that we must specify what kind of search is required with the &#8216;setSearchFunction&#8217; as shown below. The following example replaces all occurrences of &#8216;color&#8217; or &#8216;colour&#8217; with the capital &#8216;COLOR&#8217;.</p>

<div class="wp_codebox"><table><tr id="p81912"><td class="code" id="p819code12"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span> <span style="color: #0000ff;">'File/SearchReplace.php'</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$files_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$direc_to_search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test/graphics/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$search_string</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/col(o|ou)r/'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$replace_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'COLOR'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$snr</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> File_SearchReplace<span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$replace_string</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$files_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #000088;">$direc_to_search</span><span style="color: #339933;">,</span>
                              <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setSearchFunction</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;preg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doSearch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;The number of replaces done : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$snr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNumOccurences</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The <em>setSearchFunction</em> takes one of the following four options:</p>
<p>normal &#8211; default<br />
quick &#8211; use str_replace()<br />
preg &#8211; use preg_replace()<br />
ereg &#8211; use ereg_replace()</p>
<p>More information on this options can be found <a target="_blank" href="http://pear.php.net/manual/en/package.filesystem.file-searchreplace.intro.php">here</a>.</p>
<p><strong>In conclusion</strong><br />
The package can be quite useful when you want to replace large quantities of text programatically. As it works on plain strings and regular expressions, its can be quite a handy tool in many occasions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/search-replace-in-files-using-php/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Porter Stemming algorithm for search</title>
		<link>http://www.codediesel.com/php/porter-stemming-in-search/</link>
		<comments>http://www.codediesel.com/php/porter-stemming-in-search/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 07:28:37 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=724</guid>
		<description><![CDATA[In this post we will see how to use a Stemming algorithm for search purposes. A stemming algorithm lets you reduce each English input word to its basic root or stem (e.g. ‘walking’ to ‘walk’) so that variations on a word (&#8216;walks&#8217;, ‘walked’, ‘walking&#8217;) are considered equivalent when searching. This stems can than be used [...]]]></description>
			<content:encoded><![CDATA[<p>In this post we will see how to use a Stemming algorithm for search purposes. </p>
<p>A stemming algorithm lets you reduce each English input word to its basic root or stem (e.g. ‘walking’ to ‘walk’) so that variations on a word (&#8216;walks&#8217;, ‘walked’, ‘walking&#8217;) are considered equivalent when searching. This stems can than be used in a search query rather than the original words, which generally (but not always) results in more relevant search results. The main use of stemming is in keyword indexing for search. For example if you have a article or document titled &#8216;<em>blogging tips for late workers</em>&#8216; and you run it through the algorithm you will get a list of stems for the title &#8211; <em>blog, tip, late, worker</em>; under which you can than index the article or document.<br />
<span id="more-724"></span><br />
The original paper on the algorithm by Martin Porter, generally known as the Porter Stemming algorithm can be found <a target="_blank" href="http://www.tartarus.org/~martin/PorterStemmer">here</a>. The Porter Stemming algorithm essentially works by stripping suffixes from a word by using certain rules.</p>
<p>There are many implementation of the algorithm in various languages, so we will use one of those for our job. We will use a PHP5 implementation by Richard Heyes which can be downloaded from <a target="_blank" href="http://tartarus.org/~martin/PorterStemmer/php.txt">here</a>. </p>
<p>Below is an example of the use of the class, which is as simple as it can ever get.</p>

<div class="wp_codebox"><table><tr id="p72416"><td class="code" id="p724code16"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'libs/PorterStemmer.php'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$word_to_stem</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'walking'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$stem</span> <span style="color: #339933;">=</span> PorterStemmer<span style="color: #339933;">::</span><span style="color: #004000;">Stem</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$word_to_stem</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$stem</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// returns 'walk'</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The following example will create a list of stem words from a article title, also removing stop words from the list if any.</p>

<div class="wp_codebox"><table><tr id="p72417"><td class="code" id="p724code17"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/*Create a list of stem words for a article title */</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'libs/PorterStemmer.php'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;blogging tips for late workers&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Stop words to filter out. Not comprehensive though  */</span>
    <span style="color: #000088;">$stop_words</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;the&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;and&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;to&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;of&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;in&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;i&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;is&quot;</span><span style="color: #339933;">,</span>
                         <span style="color: #0000ff;">&quot;that&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;it&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;on&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;you&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;this&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;for&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;but&quot;</span><span style="color: #339933;">,</span>
                         <span style="color: #0000ff;">&quot;with&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;are&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;have&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;be&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;at&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;or&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;as&quot;</span><span style="color: #339933;">,</span>
                         <span style="color: #0000ff;">&quot;was&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;so&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;if&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;out&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;not&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$words</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$words</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$stem</span> <span style="color: #339933;">=</span> PorterStemmer<span style="color: #339933;">::</span><span style="color: #004000;">Stem</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009933; font-style: italic;">/* Remove stop words */</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stem</span><span style="color: #339933;">,</span> <span style="color: #000088;">$stop_words</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$stem_words</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$stem</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stem_words</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Which will return the following:</p>

<div class="wp_codebox"><table><tr id="p72418"><td class="code" id="p724code18"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> blog
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> tip
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> late
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> worker
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>The algorithm is basically useful when you want to index documents or extend search for morphologically related words. Although it sometimes gives amusing results, it can be quite helpful at appropriate times.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/porter-stemming-in-search/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

