1

This is my variable to be altered:

$last = 'Some string 1 foobar'

and my replace statement

$last = str_replace(['1', '2'], '', $last);

and finally the output

Some string   foobar

How do i get rid of the whitespace in between 'string' and 'foobar', my initial thought was that in my str_replace statement using '' as the replacement would also remove the whitespace but it doesnt.

To clarify I want to know how to make it Some string foobar and not Some stringfoobar.

8
  • do this $last = str_replace(['1 ', '2 '], '', $last); Commented Mar 13, 2017 at 15:40
  • alternatively, get rid of all double spaces with this preg_replace('!\s+!', ' ', $last); Commented Mar 13, 2017 at 15:41
  • 2
    You should use a regex, then you can say the whitespace around the number is optional. Commented Mar 13, 2017 at 15:42
  • @Dimi wow thanks i didnt think that any extra space IN the actual string would affect it, thanks a lot for that. I only dont use reg ex because i read that when manipulating a string its best to use str_replace() plus im not too familiar with regular expressions Commented Mar 13, 2017 at 15:44
  • @YasminFrench generally speaking, arkascha's answer is probably better solutuon. Commented Mar 13, 2017 at 15:45

2 Answers 2

1

A regular expression based approach is more flexible for such stuff:

<?php
$subject = 'Some string 1 foobar';
var_dump(preg_replace('/\d\s?/', '', $subject));

The output of above code is: string(18) "Some string foobar"

What does that do, how does it work? It replaces a pattern, not a fixed, literal string. Here the pattern is: any digit (\d) along with a single, potentially existing white space character (\s?).


A different, alternative approach would be that:

<?php
$subject = 'Some string 1 foobar';
var_dump(preg_replace('/(\s\d)+\s/', ' ', $subject));

This one replaces any sequence consisting of one or more occurrences of a digit preceded by a white space ((\s\d)+) along with a single white space by a single white blank character.

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

1 Comment

Right thanks for the explanation as well you cleared a few things up
0

If you do not want to use preg_replace then you can do something like this.

$result = 'Some string 1 foobar';
$result = str_replace(['1', '2'], '', $result);
$result = str_replace('  ', ' ', $result);

However I have to admit that I like preg_replace solution more. Not sure about the benchmark though.

5 Comments

@arkascha Then say he used the wrong variable in the second str_replace and has an syntax error because of the ].
You still mix the variables $last and $result.
@JustOnUnderMillions I tried to motivate the poster to try the code himself.
Thanks for fixing things! Looks ok now and might indeed be faster than a regular expression based attempt.
@arkascha Thats a point, but sometimes its better to be direct :-) And comments like this will stay after the question is fixed. Can irritate others ;-) Thnx for notice the error in the first place :-)

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.