Home > Article > Backend Development > PHP deletes the sub-characters at the end of the string, deletes the beginning characters, and deletes the characters at both ends_PHP tutorial
Today I encountered the following problem when dealing with deleting specific characters at both ends of a string. Let’s look at the example first
$str = 'akmumu/writtendb.json';
What I want to do is delete the akmumu at the beginning, and then delete the .json at the end, so that only useful characters are retained/writedb
First I used ltrim to delete akmumu, and then used rtrim to delete .json
It turns out that I understood trim wrong. The parameters of trim are as follows
rtrim(string,charlist)
Its parameter is a charlist, which means it is not necessarily searched in order. For example, I give a
$str = 'akmumu/writtenbsojn.json';
The result is still /write, and the /writedbsojn I want does not appear. That is to say, as long as any character in the charlist matches, it will continue like this. . .
So I used something else
str_replace, substr_replace is enough
For safety reasons, code has been added to prevent interception errors
That’s it