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.

Qirel
  • 25,449
  • 7
  • 45
  • 62
Yasmin French
  • 1,154
  • 3
  • 12
  • 25
  • do this `$last = str_replace(['1 ', '2 '], '', $last);` – Dimi Mar 13 '17 at 15:40
  • alternatively, get rid of all double spaces with this `preg_replace('!\s+!', ' ', $last);` – Dimi Mar 13 '17 at 15:41
  • 2
    You should use a regex, then you can say the whitespace around the number is optional. – chris85 Mar 13 '17 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 – Yasmin French Mar 13 '17 at 15:44
  • @YasminFrench generally speaking, arkascha's answer is probably better solutuon. – Dimi Mar 13 '17 at 15:45
  • Possible duplicate of [Remove multiple whitespaces](http://stackoverflow.com/questions/2326125/remove-multiple-whitespaces) – Rotari Radu Mar 13 '17 at 15:46
  • You can do `$last = str_replace(['1', '2',' '], ['','',' '], $last);` It remove first the 2 numbers then reduce all double spaces to a single space. The comment function will remove spaces here, so in the first single qoutes with space `' '` there a 2 spaces for real. comment will always remove it. – JustOnUnderMillions Mar 13 '17 at 15:49
  • @StackOverflow I can not put 2 spaces into single quotes? `' '`<- i have left more than one space here. (OK, the browser remove it here, in sourcecode ther are still there..arghhh) – JustOnUnderMillions Mar 13 '17 at 15:52

2 Answers2

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.

arkascha
  • 41,620
  • 7
  • 58
  • 90
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.

nicholasnet
  • 2,117
  • 2
  • 24
  • 46