Jump to content

[SOLVED] String Replace Question?


cahamilton

Recommended Posts

Would it be possible to replace multiple instances of one character for a single replacement instance. For example:

 

String 1

Name1 Surname1      Name2 Surname2              Name3 Surname3

 

What I want it to look like is:

 

String 1 Result

Name1+Surname1+Name2+Surname2+Name3+Surname3               

 

So far I'm using this method:

$string1 = str_replace(" ","+",$string 1);

 

And what this is returning is a string like this:

 

String 1

Name1+Surname1++++++Name2+Surname2+++++++++++++++Name3+Surname3++++++++++++++

 

 

Would it be possible to do this, as I want to use the result as a URL query?

Link to comment
Share on other sites

 

It's a regular expression, or regex, for short.  Pattern matching.  The pattern says look for one or more spaces or tabs in a row and replace it with a '+'.

 

The \s stands for the space or tab.  The + is the quantifier: 1 or more of the previous thing (the \s).  The ~...~ are delimiters, signifying the start and end of the pattern.  The 2nd argument of the preg_match function is what you want to replace what is matched in the pattern with. In your case, you want to match every instance of 1 or more space/tab chars in a row with a plus sign.

 

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.