Create Slug for a string

Regular expression function that replaces spaces & special characters between words with hyphens


function create_slug($string) {
	// removing special characters from the string
	$slug = preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
	// removing multiple - generated in slug
	$slug = preg_replace("/[-]+/", '-', $slug);
	// lower case slug string
	$slug = strtolower($slug);
	// removing - from the first & last place if any
	$slug = trim($slug, "-");
	return $slug;
}

echo create_slug(" This ~` is ?><:{}|][;/.,]+_)(*&^%$#@!) 'MY TITLE' for------this post. ");

Redirecting client to single domain name.

Here is a problem where a site is hosted under different virtual hosts with different domain names.

Example:
VirtualHost1 is setup for http://www.example.net
VirtualHost2 is setup for http://www.example.com

And, we want to redirect to or show only http://www.example.com domain to our clients.

Then, make some changes as below in main apache config file that is httpd.conf:

 <VirtualHost *:80>
  ServerName www.example.net
  ServerAlias example.com
  Redirect permanent / http://www.example.com/
 </VirtualHost>
<VirtualHost *:80>
 ServerName www.example.com
 DocumentRoot /usr/local/apache/htdocs
</VirtualHost>

Regular Expression

Following are the important notations of regular expression:

[ ] matches only one character position with provided in between 
- it is used to match limit range of char or number provided in [ ] e.g [0-9]
^ it is used to negate the expression if it is inside of [ ] e.g [^fF]
^ outside [ ] means to match only at the begenning of target string
$ user to match at the end of target string
. used to match any char in this position
\ escape character
? matches preceding char occurance 0 or 1 times only
* matches preceding char occurance 0 or more times only
+ matches preceding char occurance 1 or more times
{ } matches preceding char occurance n times exactly e.g [char]{n}
{n,m}