Home > Backend Development > PHP Tutorial > Intercept string PHP string subtraction code in disguise

Intercept string PHP string subtraction code in disguise

WBOY
Release: 2016-07-29 08:36:48
Original
1319 people have browsed it

A very extreme example, a way of solving a problem in disguise, recorded for later use.
How to remove the suffix from the default file name?
$fileName = a.txt
Two methods:
1: Borrow PHP’s strrchr+trim method: strrchr(string1, string2) returns the part from the end of string1 to the first encounter with string2, along with string2.
The suffix is ​​generally .XXX, so you can use $str1 = strrchr($fileName,".");
Borrowing php’s strrpos +substr method: strrpos(string1,string2) returns the position of the last occurrence of string2 in string1, substr(string1,num1,num2) intercepts the strings from num1 to num2 in string1.
Also with the help of "."
$pos = strrpos($fileName,".");
if($pos){
$fileName = substr($fileName,0,$pos);
}
This is a very extreme Example, and this processing is not very rigorous. If the name $fileName = a.b.c.d, without a suffix, it will be processed in the same way:)

The above introduces the code for intercepting strings and subtracting strings in disguise in PHP, including the content of intercepting strings. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template