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 functionstrim
,ltrim
andrtrim
? Or is that a bug?There is possibility to use the character
.
as a literal character in the above functions? For example, usingltrim
, 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
– Wallace Maxters