lifeson2112 Posted October 3, 2007 Share Posted October 3, 2007 hello, I am writing a blog program and I would like to keep intact the spaces that the author includes, but I don't want to replace every space with an entity. pretend the hyphens are space characters and the plus signs are entities; Here's what I want to do: - = - -- = -+ ---- = -+++ I tried preg_replace("/[\s](\s){1,}/"," ") ,but that doesn't work. I need something that will exclude the first space. Any tips? Quote Link to comment https://forums.phpfreaks.com/topic/71710-solved-replacing-excess-spaces-with-nbsp/ Share on other sites More sharing options...
effigy Posted October 3, 2007 Share Posted October 3, 2007 To condense multiple spaces into one use /\s+/. To ignore the first, try /(?<=\s)\s+/. Quote Link to comment https://forums.phpfreaks.com/topic/71710-solved-replacing-excess-spaces-with-nbsp/#findComment-361116 Share on other sites More sharing options...
Rithiur Posted October 5, 2007 Share Posted October 5, 2007 effigy's regex is mostly correct, except for the + quantifier, since you are looking to maintain the original amount of spaces and not remove all the extra after two. So this would probably work for you: $string = preg_replace('/(?<=\s)\s/', ' ', $string); Quote Link to comment https://forums.phpfreaks.com/topic/71710-solved-replacing-excess-spaces-with-nbsp/#findComment-362346 Share on other sites More sharing options...
lifeson2112 Posted October 6, 2007 Author Share Posted October 6, 2007 Thanks so much for the help with this. I am not well versed in regex yet and this saved me Quote Link to comment https://forums.phpfreaks.com/topic/71710-solved-replacing-excess-spaces-with-nbsp/#findComment-363100 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.