Created
March 16, 2012 04:20
-
-
Save varemenos/2048485 to your computer and use it in GitHub Desktop.
PHP - Title to Excerpt Convertion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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