PHP string disguised as a subtraction code _php tips
Source: Internet
Author: User
A very extreme example, a kind of solution to the problem of thinking, recorded, for later use.
How do I remove the suffix of the file default name?
$fileName = A.txt
Two ways:
1: Borrow PHP's Strrchr+trim method: STRRCHR (String1,string2) returns from the end of string1 to the first encounter with string2, along with string2.
Suffix general. XXX, so you can $str1 = STRRCHR ($fileName, ".");
if ($str 1) {
$fileName = Trim ($fileName, $str 1);
}
2: Borrow PHP's Strrpos+substr method: Strrpos (string1,string2) returns string2 the last occurrence in string1 (substr) Intercepts a string of string1 from NUM1 to num2.
Also with the help of "."
$pos = Strrpos ($fileName, ".");
if ($pos) {
$fileName = substr ($fileName, 0, $pos);
}
This is a very extreme example, and it is not very meticulous to do so if the name $filename = a.b.c.d, and no suffix will be processed as well:)
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.