<?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>Ruben Sargsyan &#187; matching words</title>
	<atom:link href="http://rubensargsyan.com/tag/matching-words/feed/" rel="self" type="application/rss+xml" />
	<link>http://rubensargsyan.com</link>
	<description>Personal Website</description>
	<lastBuildDate>Fri, 27 Aug 2010 07:08:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Select matching words from two texts</title>
		<link>http://rubensargsyan.com/select-matching-words-from-two-texts/</link>
		<comments>http://rubensargsyan.com/select-matching-words-from-two-texts/#comments</comments>
		<pubDate>Thu, 24 Dec 2009 08:04:58 +0000</pubDate>
		<dc:creator>Ruben Sargsyan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[matching words]]></category>

		<guid isPermaLink="false">http://rubensargsyan.com/?p=223</guid>
		<description><![CDATA[This function returns matching words array from two texts or returns false in case of not matching any words. &#60;?php function strings_intersect_words&#40;$str1,$str2&#41;&#123; $str1 = trim&#40;$str1&#41;; $str2 = trim&#40;$str2&#41;; &#160; $str1_words = explode&#40;&#34; &#34;,$str1&#41;; $str2_words = explode&#40;&#34; &#34;,$str2&#41;; &#160; $str1_words = array_unique&#40;$str1_words&#41;; $str2_words = array_unique&#40;$str2_words&#41;; &#160; $intersect_words = array&#40;&#41;; &#160; foreach&#40;$str1_words as $str1_word&#41;&#123; if&#40;array_search&#40;$str1_word,$str2_words&#41;!==false &#38;&#38; trim&#40;$str1_word&#41;!=&#34;&#34;&#41;&#123; [...]]]></description>
			<content:encoded><![CDATA[<p>This function returns matching words array from two texts or returns false in case of not matching any words.<span id="more-223"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> strings_intersect_words<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str1</span><span style="color: #339933;">,</span><span style="color: #000088;">$str2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$str1</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$str2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$str1_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;">$str1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$str2_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;">$str2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$str1_words</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_unique</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str1_words</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$str2_words</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_unique</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str2_words</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$intersect_words</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>
&nbsp;
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str1_words</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$str1_word</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_search</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str1_word</span><span style="color: #339933;">,</span><span style="color: #000088;">$str2_words</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!==</span><span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str1_word</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$intersect_words</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$str1_word</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$intersect_words</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$intersect_words</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$str1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I am Ruben Sargsyan from Armenia. I am a web developer.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$str2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://rubensargsyan.com is a web developer Ruben Sargsyan's personal website.&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$intersect_words</span> <span style="color: #339933;">=</span> strings_intersect_words<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str1</span><span style="color: #339933;">,</span><span style="color: #000088;">$str2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$intersect_words</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Output:</p>
<p>Array ( [0] => Ruben [1] => a [2] => web )</p>
]]></content:encoded>
			<wfw:commentRss>http://rubensargsyan.com/select-matching-words-from-two-texts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
