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
https://forums.phpfreaks.com/topic/155545-solved-string-replace-question/
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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.