Home  >  Article  >  Backend Development  >  php之字符串变相相减的代码_php技巧

php之字符串变相相减的代码_php技巧

WBOY
WBOYOriginal
2016-05-17 09:40:19881browse

  很极端的例子,一种变相解决的问题的思路,记录下来,以备后用。
  如何去掉文件默认名字的后缀?
  $fileName = a.txt
两种方法:
1:借用php的strrchr+trim方法:strrchr(string1,string2)返回从string1的最后开始到第一次遇到string2的部分,连同string2一起返回。
  后缀一般.XXX,所以可以$str1 = strrchr($fileName,".");
                                     if($str1){
  $fileName = trim($fileName,$str1);

2:借用php的strrpos+substr方法:strrpos(string1,string2)返回string2在string1中最后一次出现的位置,substr(string1,num1,num2)截取string1种从num1到num2的字符串。
同样借助"."
  $pos = strrpos($fileName,".");
  if($pos){
  $fileName = substr($fileName,0,$pos);
}
  这是一个很极端的例子,而且这样处理并不是非常缜密万一这个名字$fileName = a.b.c.d,而没有后缀那么会同样被处理掉:)

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