Text to Links

Replace any ‘http://’ text URLs with clickable hyperlink that open in a new window:

<?php
function textToHyperlinks( $string ) {
	return preg_replace(
		'@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@',
		'<a href="$1" target="_blank">$1</a>',
		$string
	);
}

Stack Overflow

Javascript Slugify

Javascript function to “slugify” a string:

function slugify ( str ) {
  str   = str.toLowerCase();
  str   = str.replace( /\s/gi, "-" );
  str   = str.replace( /\//gi, "-" );
  str   = str.replace( /[^-a-zA-Z0-9,&/\\s]+/ig, '' );
  
  // Trim dash from beginning, end.
  while( str.substr( 0, 1 ) == '-' )  str   = str.substr( 1 );
  while( str.substr( -1 ) == '-' )    str   = str.substr( 0, str.length -1 );
  
  return str;
}