2

I have the following String + Example + Test + Test2 and using preg_replace I would like to get the following result +Example +Test +Test2

I have been trying to use the following code

preg_replace("/\s*([\+])\s*/", "$1", $string)

but this one is returning

+Example+Test+Test2
0

1 Answer 1

5
$output = str_replace('+ ', '+', $input);

or

$output = preg_replace('/\\+\\s+/', '+', $input);
Sign up to request clarification or add additional context in comments.

2 Comments

What about multiple spaces before the +? I know the Q doesn't say it (but it implies it). Just being mischievous :)
$output = trim(preg_replace('/\s*+\s*/', ' +', $input));

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.