JSHINER Posted September 1, 2007 Share Posted September 1, 2007 If I have $town='Town Name' and it displays "Town Name" how can I get it to display "Town-Name" instead. Link to comment https://forums.phpfreaks.com/topic/67572-replacing-a-space-with/ Share on other sites More sharing options...
darkfreaks Posted September 1, 2007 Share Posted September 1, 2007 code please? Link to comment https://forums.phpfreaks.com/topic/67572-replacing-a-space-with/#findComment-339410 Share on other sites More sharing options...
d22552000 Posted September 1, 2007 Share Posted September 1, 2007 uh... preg_replace(' ','-'); Im not sure if that is exact, but you get the point. Link to comment https://forums.phpfreaks.com/topic/67572-replacing-a-space-with/#findComment-339414 Share on other sites More sharing options...
darkfreaks Posted September 1, 2007 Share Posted September 1, 2007 Preg_replace doesnt work ???if not try eregi_replace Link to comment https://forums.phpfreaks.com/topic/67572-replacing-a-space-with/#findComment-339416 Share on other sites More sharing options...
darkfreaks Posted September 1, 2007 Share Posted September 1, 2007 Try: <?php $hometown="Home Town"; $hometown = str_replace(' ', '-', $hometown); // Replaces the whitespace with a dash echo ($hometown); ?> Link to comment https://forums.phpfreaks.com/topic/67572-replacing-a-space-with/#findComment-339438 Share on other sites More sharing options...
Barand Posted September 2, 2007 Share Posted September 2, 2007 code please? I'd guess it's something like <?php echo $town; // --> Home town ?> Link to comment https://forums.phpfreaks.com/topic/67572-replacing-a-space-with/#findComment-339511 Share on other sites More sharing options...
darkfreaks Posted September 2, 2007 Share Posted September 2, 2007 lol i just tested my code it works Perfect Link to comment https://forums.phpfreaks.com/topic/67572-replacing-a-space-with/#findComment-339517 Share on other sites More sharing options...
Fadion Posted September 2, 2007 Share Posted September 2, 2007 uh... preg_replace(' ','-'); Im not sure if that is exact, but you get the point. Using a regex for such little work is a waste of time. Regular expressions need to be compiled and surely for small operations are slower then pure string manipulation. Using str_replace() is a lot better in this case. Also im not good with regex but that doesnt seem a regex pattern. Link to comment https://forums.phpfreaks.com/topic/67572-replacing-a-space-with/#findComment-339524 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.