4

Hey all. I have a string of names separated by commas. I'm exploding this string of names into an array of names with the comma as the delimiter. I need a RegEx to remove a white space(if any) only after the comma and not the white space between the first and last name.

So as an example:

$nameStr = "Sponge Bob,Bart Simpson, Ralph Kramden,Uncle Scrooge,Mickey Mouse";

See the space before Ralph Kramden? I need to have that space removed and not the space between the names. And I need to have any other spaces before names that would occur removed as well.

P.S.: I've noticed an interesting behavior regarding white space and this situation. Take the following example:

When not line breaking an echo like so:

$nameStr = "Sponge Bob,Bart Simpson, Ralph Kramden,Uncle Scrooge,Mickey Mouse";

$nameArray = explode(",", $nameStr);

foreach($nameArray as $value)
{
    echo $value;

}

The result is: Sponge BobBart Simpson Ralph KramdenUncle ScroogeMickey Mouse

Notice the white space still there before Ralph Kramden

However, when line breaking the above echo like so:

echo $value . "<br />";

The output is:

Sponge Bob

Bart Simpson

Ralph Kramden

Uncle Scrooge

Mickey Mouse

And all of the names line up with what appears to be no white space before the name.

So what exactly is PHP's behavior regarding a white space at the start of a string?

Cheers all. Thanks for replies.

8 Answers 8

4

What's with today's fixation on using regexp to solve every little problem

$nameStr = "Sponge Bob,Bart Simpson, Ralph Kramden,Uncle Scrooge,Mickey Mouse";

$nameArray = explode(",", $nameStr);

foreach($nameArray as $value)
{
    echo trim($value);

}

EDIT

PHP's behaviour re white space is to treat it as the appropriate character and do what it's told by you. HTML's behaviour (or at least that of web browsers) is rather different... and you'll need to learn and understand that difference

Sign up to request clarification or add additional context in comments.

5 Comments

Job security? After all, by using regexes to solve a problem, you end up with two problems - instant extra work.
@Marc B - :·) So answering SO questions is adversely affecting our job security
Well, I think using preg_split is more appropriate, than using explode and trim... If it can be solved by a simple regex, why not do so?
Ah, cheers Mark, thorough explanation. I gotta give a read over those functions to try and remember they exist. Thanks.
Because if it can be solved with a simple regex, it can probably be solved with built-in functions, like the above.
4

Try

$nameStr = preg_replace("/,([\s])+/",",",$nameStr);
$nameArray = explode(",", $nameStr);

This is a workable regex solution, but as others have pointed out above, a simple trim() will do the job with what you already have.

Comments

3

As you have mentioned to remove White Space only after the Comma, considering that space before comma can be left.

You can also use below:

$nameStr = "Sponge Bob,  Bart Simpson, Ralph Kramden,Uncle Scrooge,Mickey Mouse";

while (strpos($nameStr, ', ') !== FALSE) {
    $nameStr = str_replace(', ', ',', $nameStr);
}
echo $nameStr;

After this, you can simply explode it as:

$allNames = explode(',', $nameStr);

Otherwise the regex by Michael is very good.

Comments

1

Why don't you just preg_split?

$names = preg_split('~,\s*~', $names);

Comments

0

PHP couldn't care less what's in a string, unless it's parsing it for variable interpolation. A space is like any other character, an ascii or unicode value that just happens to show up as a "blank".

How are you replacing those post-comma spaces?

 $str = str_replace(', ', ',', $str);
                      ^--space

If that's not catching the space before your Ralph name, then most likely whatever's there isn't a space character (ascii 32). You can try displaying what it is with:

 echo ord(substr($nameArray['index of Ralph here'], 0, 1));

1 Comment

The poster doesn't claim that he wants to replace more than one whitespace, but he should be aware that your solution only works for exact one whitespace so that "…, Ralph" won't be replaced.
0

Have you tried it with the trim function?

$nameStr = "Sponge Bob,Bart Simpson, Ralph Kramden,Uncle Scrooge,Mickey Mouse";

$nameArray = explode(",", $nameStr);

for($i = 0; $i < count($nameArray); $i++) {
  $nameArray[$i] = trim($nameArray[$i];
}
$newNameStr = implode(',', $nameArray);

You can do this with a RegExp, but since it looks like you aren't very experienced with RegExp you shouldn't use them, because when doing it wrong they cost a good chunk of performance.

Comments

0

An easy solution is to avoid using regex and apply the trim function:

$nameStr = 'Bart Simpson, Lisa Simpson,Homer Simpson, Madonna';
$names = explode(',', $nameStr);
foreach($names as &$name) {
  $name = trim($name);
}

//this also doesn't falter when a name is only one word

Comments

0

this one works for me

$string = "test1, test2, test3, test4, , ,";    
array_map('trim', array_filter(explode(',', $string), 'trim'))

output

=> [
     "test1",
     "test2",
     "test3",
     "test4",
   ]

1 Comment

Please share more details - to me, this looks like an alternate version of the accepted answer which is about ten years old

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.