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. 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. 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" 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. 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); } 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 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
Archived
This topic is now archived and is closed to further replies.