suresh_kamrushi Posted August 21, 2012 Share Posted August 21, 2012 I have a string like below: %s% % % sent you a Mystery Gift with I want to remove white spaces between % (percent) sign only by using regular expression so that string will look like %s%%% sent you a Mystery Gift with Thanks in Advance Quote Link to comment https://forums.phpfreaks.com/topic/267362-need-to-remove-white-sapces-with-regular-expression/ Share on other sites More sharing options...
MMDE Posted August 21, 2012 Share Posted August 21, 2012 nvm Quote Link to comment https://forums.phpfreaks.com/topic/267362-need-to-remove-white-sapces-with-regular-expression/#findComment-1371038 Share on other sites More sharing options...
Christian F. Posted August 21, 2012 Share Posted August 21, 2012 Why not just use str_replace ()? RegExp seems to be a bit overkill for something as simple as this. Quote Link to comment https://forums.phpfreaks.com/topic/267362-need-to-remove-white-sapces-with-regular-expression/#findComment-1371048 Share on other sites More sharing options...
MMDE Posted August 21, 2012 Share Posted August 21, 2012 Why not just use str_replace ()? RegExp seems to be a bit overkill for something as simple as this. So what do you suggest the search string to be, and the replacement? echo str_replace('% %', '%%', '%s% % % sent you a Mystery Gift with'); Was my original thought, but then I noticed it prints this: %s%% % sent you a Mystery Gift with I guess you can do: echo str_replace('% ', '%', '%s% % % sent you a Mystery Gift with'); Quote Link to comment https://forums.phpfreaks.com/topic/267362-need-to-remove-white-sapces-with-regular-expression/#findComment-1371049 Share on other sites More sharing options...
Christian F. Posted August 21, 2012 Share Posted August 21, 2012 No need to make it more complex than it has to be: str_replace (' %', '%', $string) Quote Link to comment https://forums.phpfreaks.com/topic/267362-need-to-remove-white-sapces-with-regular-expression/#findComment-1371050 Share on other sites More sharing options...
MMDE Posted August 21, 2012 Share Posted August 21, 2012 No need to make it more complex than it has to be: str_replace (' %', '%', $string) Ah, yes, I came up with that probably a couple of seconds after you. xD echo str_replace('% ', '%', '%s% % % sent you a Mystery Gift with'); Though I placed the space after the first %, you could choose one or both to really remove all of them. Quote Link to comment https://forums.phpfreaks.com/topic/267362-need-to-remove-white-sapces-with-regular-expression/#findComment-1371051 Share on other sites More sharing options...
suresh_kamrushi Posted August 21, 2012 Author Share Posted August 21, 2012 Thanks Guys!! With str_replace i already figure it out like this $string = "%s% % % sent you a Mystery Gift with"; $possibleSearch = array("% %","%% %","% %%"); $possibleValue = array("%%","%%%","%%%"); $newString = str_replace($possibleSearch,$possibleValue,$string); But really i need it with reg_replace function. Any answer appreciated!!! Thanks in Advance Quote Link to comment https://forums.phpfreaks.com/topic/267362-need-to-remove-white-sapces-with-regular-expression/#findComment-1371053 Share on other sites More sharing options...
Christian F. Posted August 21, 2012 Share Posted August 21, 2012 That begs the question: Why do you think you need it as a RegExp? Also, please see my last post again. Quote Link to comment https://forums.phpfreaks.com/topic/267362-need-to-remove-white-sapces-with-regular-expression/#findComment-1371054 Share on other sites More sharing options...
MMDE Posted August 21, 2012 Share Posted August 21, 2012 Thanks Guys!! With str_replace i already figure it out like this $string = "%s% % % sent you a Mystery Gift with"; $possibleSearch = array("% %","%% %","% %%"); $possibleValue = array("%%","%%%","%%%"); $newString = str_replace($possibleSearch,$possibleValue,$string); But really i need it with reg_replace function. Any answer appreciated!!! Thanks in Advance We've both posted you two working codes. If you want to use more resources, just replace str in the function name with preg, and place a / at the start and the end of the search string (pattern). Quote Link to comment https://forums.phpfreaks.com/topic/267362-need-to-remove-white-sapces-with-regular-expression/#findComment-1371055 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.