rob.m
rob.m

Reputation: 10571

How to remove the very first empty space at the start of a string?

I have a string like:

" United States" and I'd like it to be "United States"

I use the following to replace the string for other reasons but I cannot figure out how to tell it to only remove the first empty space:

$title = usp_get_meta(false, 'usp-custom-8');
update_post_meta( $post->ID, 'usp-custom-8', str_replace('n Argentinan', 'Argentina', $title ));

I know I could use something like $str = ltrim($str); but I am not sure how to put it there and I don't want to risk to run the loop and create a disaster as it will go through my whole db.

In theory I could also run it in another bit of code that I have:

        $stateList = usp_get_meta(false, 'usp-custom-8');
        $stateList = ltrim($stateList);
        $stateList = explode(',', $stateList);
        foreach($stateList as $state) {
          array_push($countries, $state);
        }

But I get Array as a value. when using ltrim there.

So either I make it run a loop as the first example or that last piece of code, yet how can I remove the first empty space from the string?

Upvotes: 5

Views: 1432

Answers (6)

hanshenrik
hanshenrik

Reputation: 21463

The suggested ltrim approach will not delete the first empty space. Instead it would delete all the spaces at the start of the string, i.e. ltrim("foo bar") would return foo bar, not foobar (it would not delete the first empty space in the string)

Instead use preg_replace with $limit, $str=preg_replace("/\s/u","",$str,1); - Which would remove the first space in $str, regardless of where the first space is.

update_post_meta( $post->ID, 'usp-custom-8', preg_replace("/\s/u","",$title,1));

https://www.reddit.com/r/MaliciousCompliance/

Upvotes: 4

Shahnawaz Kadari
Shahnawaz Kadari

Reputation: 1571

If $title is string and you only want to remove white space at the beginning of string, you can try....

Code

$title = " United States";
if(strpos($title," ") === 0 ){
  $title = substr($title, -(strlen($title)-1));
}
var_dump($title);

I'm using === strict check, sometime strpos return false.

Output

string(13) "United States"

Demo

Upvotes: 1

Pedro Lobito
Pedro Lobito

Reputation: 98901

I'm not entirely sure of what you're trying to do, but in theory you can use array_map() with the ltrim() function, or even trim(), as argument after exploding $stateList, i.e. (untested):

$stateList = array_map("ltrim",  explode(',', $stateList));
# loop...

  1. explode() - Split a string by a string arrays
  2. array_map() - Applies the callback to the elements of the given arrays
  3. ltrim() - Strip whitespace (or other characters) from the beginning of a string
  4. trim() - Strip whitespace (or other characters) from the beginning and end of a string

Upvotes: 5

MrSmile
MrSmile

Reputation: 1233

Or you can just use preg_replace:

$stateList = "     United States  ,   Argentina   ,  France      ";

$stateList = preg_replace("#\s*,\s*#", ',', trim($stateList) );

print_r(explode(",", $stateList));

output is:

Array
(
    [0] => United States
    [1] => Argentina
    [2] => France
)

Upvotes: 2

Andrei Todorut
Andrei Todorut

Reputation: 4526

Use array_map function http://php.net/manual/en/function.array-map.php

function removeLeftEmptySpace($str) {
    return ltrim($str);
}

$arr = [' United states', 'Boston'];
print_r(array_map('removeLeftSpace',$arr));

Upvotes: 4

Dirk J. Faber
Dirk J. Faber

Reputation: 4701

You could loop and trim and put this all in a new array, like so:

$countries  = [' United States', 'Argentina', ' France'];
$countriesClean = array();

    foreach($countries as $country) {
      $cleanCountry = ltrim($country);
      array_push($countriesClean, $cleanCountry);
    }

In this case, Argentina is the only country name that does not require a cleanup. Demo

Upvotes: 3

Related Questions