note 54517 deleted from function.preg-replace by tularis
| From: | tularis@php.net | Date: | Sun, 18 Jun 2006 13:27:11 +0000 |
| Subject: | note 54517 deleted from function.preg-replace by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-114315@lists.php.net to get a copy of this message | ||
Note Submitter: shazow at gmail dot com
----
Here's a function that will take in a string and make it URL-friendly. This is useful for
basing dynamically created paths on given titles (eg. in my case, I label subdirectories based on
the name of the category they represent).
<? php
function makeUrlFriendly($input) {
// Replace spaces with underscores
$output = preg_replace("/\s/e" , "_" , $input);
// Remove non-word characters
$output = preg_replace("/\W/e" , "" , $output);
return $output;
}
// Example useage:
echo makeUrlFriendly("I'm some really long title !!1"); // Gives:
Im_some_really_long_title_1
echo makeUrlFriendly("Weird chars too: £©§ &"); // Gives: Weird_chars_too__amp
?>
- shazow