Home  >  Article  >  Backend Development  >  How to remove file extension in php

How to remove file extension in php

王林
王林Original
2020-08-20 15:26:343024browse

php method to remove the file suffix: first use the strrchr() function to find the position where the file suffix appears in the file name; then use the str_replace() function to replace the file suffix.

How to remove file extension in php

str_replace() function replaces some characters in a string (case sensitive).

(Recommended tutorial: php graphic tutorial)

Syntax:

str_replace(find,replace,string,count)

Parameters:

  • find Required. Specifies the value to look for.

  • replace Required. Specifies the value to replace the value in find .

  • #string Required. Specifies the string to be searched for.

  • #count Optional. A variable counting the number of substitutions.

strrchr() function finds the last occurrence of a string in another string and returns all characters from that position to the end of the string.

Grammar:

strrchr(string,char)

(Learning video recommendation: php video tutorial)

Parameters:

  • ## string Required. Specifies the string to be searched for.

  • #char Required. Specifies the characters to search for. If the argument is a number, then searches for characters matching the numeric ASCII value.

Implementation code:


<?php 
$filename="help.php";
$filename=str_replace(strrchr($filename, "."),"",$filename);
echo $filename;
?>

Output result:


help

The above is the detailed content of How to remove file extension in php. For more information, please follow other related articles on the PHP Chinese website!

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