varemenos
3/16/2012 - 4:20 AM

PHP - Title to Excerpt Convertion

PHP - Title to Excerpt Convertion

<?php
	// sample title
	$title = "the, *t.ru/e ;h'or']ror} (2)3!@ # $ % ^ & (* )^ ) _+-0(*)*%(&!^";

	// find and replace any whitespace characters with underscores
	$excerpt = preg_replace('/\s/', '_', $title);
	// find an replace any none alphanumeric or underscore characters with any empty string ''
	$excerpt = preg_replace('/[^a-zA-Z0-9_]/', '', $excerpt);
	// while there is a '__' string inside the string, keep replacing the '__' with '_'
	while (strpos($excerpt, '__') !== false){
	    $excerpt = str_replace('__', '_', $excerpt);
	}
	// turn the characters to lowercase
	$excerpt = strtolower($excerpt);

	// $excerpt = "the_true_horror_23_0"
?>