Php deletes the substring at the end of a string

Source: Internet
Author: User

Today, I want to deal with the subcharacters that were added due to mistakes, such as linzimu.apk.apk.

At first, rtrim was used. In fact, this function is derived from trim, trim is used to remove the final part, rtrim is used to remove the end of the character, and ltrim is used to remove the character header.

Use the following code:


[Php]
$ Out = rtrim(inin,'.apk ');

$ Out = rtrim(inin,'.apk ');
The output result is linzimu. Well, I want to [php] view plaincopyprint?
$ Out. = '.apk'

$ Out. = '.apk'


That's what I want.

After that, we should check whether the file exists before uploading it to the server. The result is that 4 K of data does not exist.


I finally found the cause after searching for a long time. In fact, I did not read the PHP manual carefully.

Ltrim is followed by charlist instead of $ substring

That is to say, the character list is not necessarily followed by this order,

For example, a in linzimua, apk, and apk will be removed.

The principle of removal is that all the matching operations are always removed until the first non-matching occurs.


So you can use another method immediately.

Train of Thought 1: The thought can be changed to remove the string, can be changed to replace the string, replace with NULL

Use

[Php]
Substr_replace ($ test, '',-4 );

Substr_replace ($ test, '',-4 );
Idea 2: You can also extract the desired string as a sub-string.


[Php]
$ Test = "lin.apk.apk ";
Preg_match ("# ^ (. +) \. (. +) \. # I", $ test, $ m );
Echo $ m [1];

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.