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

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

WBOY
WBOYOriginal
2016-07-21 15:03:02949browse

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

Copy Code The code is as follows:

if(strpos($str,'akmumu/') !== FALSE
$str = substr($str,7);
if(strpos($str,'.json') !== FALSE)
{
if(substr($str,-5,5) == '.json')
{
$str = substr_replace($str,'',-5);
}
}
}

That’s it

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327881.htmlTechArticleToday I encountered the following problem when dealing with deleting specific characters at both ends of a string. Let’s look at the example SPAN style=" FONT-SIZE: 18px"/SPAN $str = 'akmumu/writedb.json'; What I want to do is delete the start...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn