note 54517 added to function.preg-replace
| From: | shazow at gmail dot com | Date: | Wed, 06 Jul 2005 21:19:00 +0000 |
| Subject: | note 54517 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-91553@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54517
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54517&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54517&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54517&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54517&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54517&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54517&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54517&report=yes
Search -- http://master.php.net/manage/user-notes.php