<?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>edrackham &#187; Mod_Rewrite</title>
	<atom:link href="http://edrackham.com/category/mod_rewrite/feed/" rel="self" type="application/rss+xml" />
	<link>http://edrackham.com</link>
	<description>PHP, MySQL, JavaScript and Other Web Tutorials!</description>
	<lastBuildDate>Thu, 04 Aug 2011 08:30:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to make URL Safe strings for mod_rewrite using PHP</title>
		<link>http://edrackham.com/php/how-to-make-url-safe-strings-for-mod_rewrite-using-php/</link>
		<comments>http://edrackham.com/php/how-to-make-url-safe-strings-for-mod_rewrite-using-php/#comments</comments>
		<pubDate>Sun, 11 Jul 2010 17:18:18 +0000</pubDate>
		<dc:creator>Ed</dc:creator>
				<category><![CDATA[Mod_Rewrite]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[mod rewrite]]></category>
		<category><![CDATA[one liner]]></category>
		<category><![CDATA[preg replace]]></category>
		<category><![CDATA[url safe]]></category>

		<guid isPermaLink="false">http://edrackham.com/uncategorized/how-to-make-url-safe-strings-for-mod_rewrite-using-php/</guid>
		<description><![CDATA[It&#8217;s relatively easy to make URL safe strings for use by mod_rewrite. Let&#8217;s use the example that you have a form that adds a new blog post to your site. When the user submits this form, you want to generate a URL safe string (based on the title of the blog post) for mod_rewrite to [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s relatively easy to make URL safe strings for use by mod_rewrite. Let&#8217;s use the example that you have a form that adds a new blog post to your site. When the user submits this form, you want to generate a URL safe string (based on the title of the blog post) for mod_rewrite to use. This little snippet will show you how this can be achieved in one line of code in PHP.<br />
<span id="more-31"></span></p>
<p>The following function shows just how easy it is to generate a URL safe string for mod_rewrite. I&#8217;ve even included a demo of this code below.</p>
<p><div align="center" style="margin-bottom: 20px;">
<script type="text/javascript"><!--
google_ad_client = "pub-4159646232668987";
google_ad_slot = "5003066961";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></p>
<h3>Try it out&#8230;</h3>
<p><iframe frameborder="0" src="http://edrackham.com/tutorials/making-mod-rewrite-strings-with-php/?iframe" height="80px" width="460px" scrolling="no">Your browser doesn&#8217;t support IFRAMES. You can see the URL safe generator <a href="http://edrackham.com/tutorials/making-mod-rewrite-strings-with-php/" title="Simple Online URL Safe Generator">here</a>.</iframe></p>
<pre name="code" class="php">function MakeURLSafeString($string){
    return trim(preg_replace('/[-]{2,}/', '-', preg_replace('/[^a-z0-9]+/', '-', strtolower($_POST['TheString']))), '-');
}</pre>
<p>That&#8217;s all there is to it! To break it down a little, you could have written it as per the following:</p>
<pre name="code" class="php">function MakeURLSafeString($string){
    $string = strtolower($string); // Makes everything lowercase (just looks tidier).
    $string = preg_replace('/[^a-z0-9]+/', '-', $string); // Replaces all non-alphanumeric characters with a hyphen.
    $string = preg_replace('/[-]{2,}/', '-', $string); // Replaces one or more occurrences of a hyphen, with a single one.
    $string = trim($string, '-'); // This ensures that our string doesn't start or end with a hyphen.
    return $string;
}</pre>
<p><div align="center" style="margin-bottom: 20px;">
<script type="text/javascript"><!--
google_ad_client = "pub-4159646232668987";
google_ad_slot = "5003066961";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></p>
]]></content:encoded>
			<wfw:commentRss>http://edrackham.com/php/how-to-make-url-safe-strings-for-mod_rewrite-using-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

