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. Quote 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? Quote 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. Quote 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 Quote 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); ?> Quote 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 ?> Quote 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 Quote 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. Quote Link to comment https://forums.phpfreaks.com/topic/67572-replacing-a-space-with/#findComment-339524 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.