Home > Article > Backend Development > PHP deletes the sub-characters at the end of the string, deletes the starting character, and deletes the characters at both ends (implementation code)_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
Start I used ltrim to delete akmumu, and then used rtrim to delete .json
It turned out that I understood trim wrong. The parameters of trim are as follows
rtrim(string,charlist)
His parameters are charlist, which is not necessarily the case. Search in order, for example, if I give a
$str = 'akmumu/writedbsojn.json';
the result is still /write, the /writedbsojn I want does not appear, that is to say, as long as any character in the charlist matches It goes on like this. . .
So I just use something else
str_replace, substr_replace
For safety reasons, I added code to prevent interception errors