<?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>arkinEx Codex &#187; badword</title>
	<atom:link href="http://www.arkinex.com/contains/badword/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.arkinex.com</link>
	<description>the place where all my code can be viewed</description>
	<lastBuildDate>Wed, 04 Nov 2009 20:39:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Simple Swear/Badword Filtering tutorial</title>
		<link>http://www.arkinex.com/php/tutorials/15/simple-swear-filter-tutorial/</link>
		<comments>http://www.arkinex.com/php/tutorials/15/simple-swear-filter-tutorial/#comments</comments>
		<pubDate>Wed, 28 Nov 2007 23:43:26 +0000</pubDate>
		<dc:creator>arkin</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[badword]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[swearword]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.arkinex.com/php/tutorials/15/simple-swear-filter-tutorial/</guid>
		<description><![CDATA[In this tutorial you&#8217;ll learn how to censor the swear words and profanity on your website. You could even use this tutorial to prevent people making posts about your competitors. * Contains strong language * First things first, the functions we will be using in this tutorial. file() str_replace() str_repeat() foreach() { The nice things [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial you&#8217;ll learn how to censor the swear words and profanity on your website.</p>
<p>You could even use this tutorial to prevent people making posts about your competitors.</p>
<p><strong>* Contains strong language *</strong></p>
<p><span id="more-15"></span><br />
First things first, the functions we will be using in this tutorial.</p>
<ul>
<li><a href="http://php.net/file">file()</a></li>
<li><a href="http://php.net/str_replace">str_replace()</a></li>
<li><a href="http://php.net/str_repeat">str_repeat()</a></li>
<li><a href="http://php.net/foreach">foreach() {</a></li>
</ul>
<p>The nice things about this tutorial are that it will teach you how to load a file as an array using the file() function, it will also cache that file using global to see if the censored words have already been loaded and it will replace them with their counterpart (word for example as either w***d or *****, both 5 letters).</p>
<p>To start things out, were going to create a file with the swearwords.<br />
Alternitavely, you could create a string, but we&#8217;ll talk about that later.<br />
If you would like a list of many common swearwords, check out my <a href="http://www.arkinex.com/blog/swear-words-text-file-dl/" target="_blank">Swear Word Text File</a>.</p>
<p>swearwords.txt</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">shit
bitch
arkinex
google</pre></div></div>

<p>then, the php part of the censoring.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* arkinex-censor.php v1.0 - www.arkinex.com */</span>
<span style="color: #666666; font-style: italic;">/* don't forget to create swearwords.txt */</span>
<span style="color: #000000; font-weight: bold;">function</span> censor<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span><span style="color: #000088;">$all</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// Global checks the cache to see if $censor_words already exists.</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$censor_words</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// If there is no cache aka censor_words is empty or is not an array</span>
	<span style="color: #666666; font-style: italic;">// Then load the swearwords.txt</span>
	<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;">$censor_words</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">||!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$censor_words</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$censor_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>
		<span style="color: #000088;">$censor_words</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'swearwords.txt'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$censors</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: #666666; font-style: italic;">// Check if the censor words are empty.</span>
	<span style="color: #666666; font-style: italic;">// Returns an invisible error and the string if they are.</span>
	<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;">$censor_words</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'&lt;!--error--&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$string</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// This foreach loops through each censor word</span>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$censor_words</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// This checks if you want all the word censored or partial</span>
	<span style="color: #666666; font-style: italic;">// Str_repeat repeats * for the number of letters (minus 2)</span>
	<span style="color: #666666; font-style: italic;">// Substr grabs the first and last letter (the 2)</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$all</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$censors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #990000;">str_repeat</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'*'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// If you want to censor the full word..</span>
	<span style="color: #666666; font-style: italic;">// It Str_repeats for the length of the entire word</span>
		<span style="color: #b1b100;">else</span> <span style="color: #000088;">$censors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_repeat</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'*'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #666666; font-style: italic;">// Str_replace replaces all the words with their censors</span>
	<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$censor_words</span><span style="color: #339933;">,</span> <span style="color: #000088;">$censors</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// This returns the string for display</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> censor<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The cat sat on the arkinex&quot;</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: #666666; font-style: italic;">// Outputs: The cat sat on the a*****x</span></pre></div></div>

<p>Now to the example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> censor<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The cat sat on the arkinex&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Outputs: The cat sat on the a*****x</span>
<span style="color: #b1b100;">echo</span> censor<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;You should all go to arkinex!&quot;</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: #666666; font-style: italic;">// Outputs: You should all go to *******!</span></pre></div></div>

<p>You can download the full snippet, <a class="file-php" href="http://www.arkinex.com/resources/downloads/swearword-censor.phps">swearword-censor.phps</a>.<br />
Reply to this article if you need additional help or have any idea&#8217;s.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arkinex.com/php/tutorials/15/simple-swear-filter-tutorial/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>
