Home > Backend Development > PHP Problem > How to remove file extension in php

How to remove file extension in php

王林
Release: 2023-03-04 21:10:01
Original
3132 people have browsed it

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)
Copy after login

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)
Copy after login

(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;
?>
Copy after login

Output result:


help
Copy after login

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!

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