11

If I do:

ltrim('53-34567', '53-');
ltrim('53+34567', '53+');
ltrim('53*34567', '53*');

I get 4567 as the result and not 34567. What's the explanation for this behavior?

Boann
  • 48,794
  • 16
  • 117
  • 146
sica07
  • 4,896
  • 8
  • 35
  • 54
  • 1
    The ltrim() function removes whitespace or other predefined characters from the left side of a string. [Link](https://www.w3schools.com/php/func_string_ltrim.asp) – GYaN May 04 '18 at 11:51
  • 4
    Possible duplicate of [ltrim strips more than needed](https://stackoverflow.com/questions/5654852/ltrim-strips-more-than-needed), or [PHP ltrim behavior with character list](https://stackoverflow.com/questions/9050917/php-ltrim-behavior-with-character-list) – Syscall May 04 '18 at 12:04
  • 1
    The second parameter is a _set_ of characters, not a _sequence_ of characters. – Dennis Williamson May 04 '18 at 18:50

3 Answers3

24
ltrim('53-34567', '53-');

There is a 5 at the begining of '53-34567' so it is removed.

There is a 3 at the begining of '3-34567' so it is removed.

There is a - at the begining of '-34567' so it is removed.

There is a 3 at the begining of '34567' so it is removed.

There is nothing in '53-' at the begining of '4567' so it stopped.

This is the same behaviour than a trim() by removing unwanted trailing characters. In example, trim(" lots of spaces "); will return "lots of spaces" by removing trailing and leading spaces but will keep the inner ones.

Cid
  • 14,968
  • 4
  • 30
  • 45
  • 1
    Very clear! Thank you! – sica07 May 04 '18 at 11:56
  • 3
    This is a very clear and good explanation of what is happening. Far more useful than throwing around the `character_mask` definition. If I may suggest something, add "Continue removing `5` or `3` or `-` from the left side of `53-34567`." somewhere in your answer. – MonkeyZeus May 04 '18 at 12:56
17

The second argument of ltrim() is as follows

character_mask
You can also specify the characters you want to strip, by means of the character_mask parameter. Simply list all characters that you want to be stripped.

It's used to specify which characters to trim, yours include 3. If you know what you want to extract, you can use other string functions.

As to why the other 5 is then not removed, see this comment: http://php.net/manual/en/function.ltrim.php#118221

Gerben Jacobs
  • 4,515
  • 3
  • 34
  • 56
  • I don't think I understand :) In this case, why doesn't `5` get trimmed also? The end result contains `5`. Also, if I use `53` as the second argument (without any operator sign), I get the `+34567` result. – sica07 May 04 '18 at 11:54
  • 1
    @sica07 : try `ltrim('52*34567', '52*');` you will get your answer – Niklesh Raut May 04 '18 at 11:56
  • 1
    To be more precise, trim() excludes the characters listed in the second parameter (independently of how often they occur) and not the argument string. In the OT's example the three lines could be reduced to `ltrim($str, '+3-5*')` (or any other permutation of the characters) and work the same. – Dormilich May 04 '18 at 11:56
6

This is because ltrim trims characters from a second param.

How it works:

  1. start read a string(first param) from left.
  2. check if first-left char is in a list of 'character-to-trim' list.
  3. If 2 is YES - trim it and go to step 1.
  4. if 2 is NO - exit.
Sergei Karpov
  • 106
  • 1
  • 5