Home  >  Article  >  Backend Development  >  PHP deletes subcharacters at the end of a string_PHP tutorial

PHP deletes subcharacters at the end of a string_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:11:081047browse

Today I want to deal with the extra sub-characters that were originally added due to mistakes, such as linzimu.apk.apk.

Initially, rtrim was used. In fact, this function is a derivative of trim. Trim removes the ending, while rtrim removes the end of the character. There is also ltrim, which removes the head of the character.

Use the following code:


[php]
$out = rtrim($in,'.apk');

$out = rtrim($in,'.apk');
The output result is linzimu, so I think that’s good, then I will just use [php] view plaincopyprint?
$out .= '.apk'

$out .= '.apk'


That’s what I want

To be on the safe side, check whether the file exists before uploading it to the server. As a result, 4k of the more than 2W data does not exist.


After searching for a long time, I finally found the reason. In fact, I didn't read the PHP manual carefully.

ltrim is followed by charlist instead of $substring

That is to say, what follows is a list of characters, not necessarily in this order,

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

The principle of removal is to keep matching and removing until the first one does not match.


So immediately switch to another method, you can use

Idea 1: You can change the thinking to remove the string, you can change it to a replacement string, replace it with NULL

Use

[php]
substr_replace($test,'',-4);

substr_replace($test,'',-4);
Idea 2: It can also be changed to extract the string you want as a substring


[php]
$test = "lin.apk.apk";
preg_match("#^((.+).(.+)).#i", $test,$m);
echo $m[1];

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477396.htmlTechArticleToday I want to deal with the extra sub-characters that were originally added due to mistakes, such as linzimu.apk.apk. At first, rtrim was used. In fact, this function is a derivative of trim. Trim removes the ending, while rtrim removes...
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