JustinK101 Posted December 8, 2008 Share Posted December 8, 2008 I need a way to take a string of text: example: "Computers / Hardware" And make it lowercase, remove any spaces (replace with _), and strip any forward or backward slashes. So the example becomes: example fixed: "computers_hardware" Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/135994-format-string-replace-whitespaces-with-slashes-remove-forwardbackward-slashes/ Share on other sites More sharing options...
premiso Posted December 8, 2008 Share Posted December 8, 2008 <?php $string = "COMPUTERS / HARDWARE"; $string = preg_replace("/[\/]/", "_", str_replace(" ", "", strtolower($string))); echo $string; die(); ?> Should work. Quote Link to comment https://forums.phpfreaks.com/topic/135994-format-string-replace-whitespaces-with-slashes-remove-forwardbackward-slashes/#findComment-709011 Share on other sites More sharing options...
JustinK101 Posted December 8, 2008 Author Share Posted December 8, 2008 Premiso, Thanks for the reply. Worked except on the case: example: "Xbox Games" output: "xboxgames" Is it possible to put an underscore between xbox and games, so its "xbox_games" Quote Link to comment https://forums.phpfreaks.com/topic/135994-format-string-replace-whitespaces-with-slashes-remove-forwardbackward-slashes/#findComment-709024 Share on other sites More sharing options...
DarkWater Posted December 8, 2008 Share Posted December 8, 2008 <?php $string = "Xbox Games"; $string = strtolower($string); $string = preg_replace("/\s+/", "_", $string); $string = str_replace(array('\\', '/'), '', $string); $string = preg_replace("/_+/", "_", $string); echo $string; ?> Might want to put that into a function. Quote Link to comment https://forums.phpfreaks.com/topic/135994-format-string-replace-whitespaces-with-slashes-remove-forwardbackward-slashes/#findComment-709031 Share on other sites More sharing options...
JustinK101 Posted December 8, 2008 Author Share Posted December 8, 2008 Darkwater, Perfect, you are the man. Thanks greatly. Below is the final function for others to use if they need: function url_text($text) { $text = strtolower($text); $text = preg_replace("/\s+/", "_", $text); $text = str_replace(array('\\', '/'), '', $text); $text = preg_replace("/_+/", "_", $text); return($text); } Quote Link to comment https://forums.phpfreaks.com/topic/135994-format-string-replace-whitespaces-with-slashes-remove-forwardbackward-slashes/#findComment-709040 Share on other sites More sharing options...
premiso Posted December 8, 2008 Share Posted December 8, 2008 <?php $string = "XBOX HARDWARE"; $string = preg_replace("/\s+/ ", "_", preg_replace("/[\/]/", "", strtolower($string))); echo $string; die(); ?> A revised version, should work Quote Link to comment https://forums.phpfreaks.com/topic/135994-format-string-replace-whitespaces-with-slashes-remove-forwardbackward-slashes/#findComment-709043 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.