What is the use of "." (dot) in PHP’s ltrim, rtrim and Trim functions?

Asked

Viewed 133 times

3

I was here doing a feature, where I needed to remove some characters from the beginning of a string, and I wasn’t really in the mood to use regex for that. I tried to use the function ltrim, since it serves to remove characters that are to the left of a string. I’ve always used it to remove prefixes.

The problem was I just found out that the character . serves as a kind of wild card.

In my case, I have a function that returns the name of the file used in the project, which always has .env.nome_do_cliente. The problem arose when you wanted to get the client name based on the name of that file.

Exemplifying

$nome_do_arquivo = '.env.exemplo';
$cliente = ltrim($nome_do_arquivo, '.env.');
// Resultado: "xemplo" 

Example in IDEONE

My intention with the above code was to remove only the .env. of the string. Of course I know you have how to solve this with substr and ignore even the amount of characters desired, but the behavior of this function made me curious. I wondered if it was an expected behavior or if it was a bug.

On that note, I have a few questions:

  • If indeed the character . is interpreted as something special in these functions, what is the purpose of it in the functions trim, ltrim and rtrim ? Or is that a bug?

  • There is possibility to use the character . as a literal character in the above functions? For example, using ltrim, there would be some way to remove only the character . from the beginning of a string? (I tried to use \. and it didn’t work).

  • And, besides the ., there are other characters that can be interpreted as "wildcards/specials" by this function?

  • Related: https://answall.com/questions/23335

1 answer

4


Actually the second parameter contains the list of characters that will be removed, and nay is interpreted as a substring. Example:

echo ltrim('abcaabdef', 'cba'); // def

In the above example, I am saying to remove any character from the "list" 'cba' - that is, it traverses the string 'abcaabdef' and will remove any character that is a "c", or "b", or "a". When he finds the first that is none of this list (in this case, the "d"), he stops and returns from then on (i.e., "def").


So, when using '.env.' you’re saying "remove a stitch, or the letter e, or n, or v, or a dot". So it also removes the "e" before the "x". And when arriving at "x", it stops, because "x" is not any of the characters entered in the second parameter.

So the dot is not interpreted as joker. So much so that if I change the order of the characters, the result is the same:

$nome_do_arquivo = '.env.exemplo';
$cliente = ltrim($nome_do_arquivo, 'vn.e');
echo $cliente; // xemplo

For the record, the dot can have special meaning if used like this:

echo ltrim('abcaabdef', 'a..d'); // ef

In the case, a..d means "any character between a and d". But from what it says on documentation, seems to be the only special syntax, anything else is treated as a normal character.


rtrim and trim work in the same way (the second parameter is treated as a list of characters to be removed, not as a substring), the difference being that rtrim remove only from the end and trim remove from start and end:

echo rtrim('abc123abcaab', 'cba'); // abc123
echo trim('abc123abcaab', 'cba'); // 123
echo ltrim('abc123abcaab', 'cba'); // 123abcaab
  • 1

    I was halfway through my answer, but you were faster. : P

Browser other questions tagged

You are not signed in. Login or sign up in order to post.