PHP deletes the character at the end of the string, deletes the start character, deletes the two characters

Source: Internet
Author: User

Today, when dealing with a specific character at both ends of the string, you encounter the following problem, first look at the example
<span style= "font-size:18px" ></SPAN>
$str = ' Akmumu/writedb.json ';
What I'm going to do is delete the starting Akmumu, and then delete the. JSON at the end, so that only useful characters are preserved/writedb
Start the LTrim I use to remove Akmumu, and then use RTrim to remove it. JSON
It turns out that I understand the trim error, and the trim parameters are as follows
RTrim (String,charlist)
His arguments are charlist, which is not necessarily in order, like I give a
$str = ' Akmumu/writedbsojn.json ';
The result is still/write, I want the/WRITEDBSOJN did not appear, that is, as long as the charlist of any character matching is always so down ...
So I used something else.
Str_replace,substr_replace can
For security reasons, add code to prevent further interception of errors

Copy Code code 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, okay?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.