Skip to content

Instantly share code, notes, and snippets.

@varemenos
Created March 16, 2012 04:20
Show Gist options
  • Save varemenos/2048485 to your computer and use it in GitHub Desktop.
Save varemenos/2048485 to your computer and use it in GitHub Desktop.
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"
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment