Jump to content

Need to remove white sapces with regular expression


suresh_kamrushi

Recommended Posts

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');

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.