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

PHP deletes the sub-characters at the end of the string, deletes the beginning characters, and deletes the characters at both ends_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:17:05870browse

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

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/372204.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 first. -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