Home  >  Article  >  Backend Development  >  How to remove the first and last characters of a string in php

How to remove the first and last characters of a string in php

王林
王林Original
2020-09-19 16:12:534864browse

How to remove the first and last characters of a string in php: You can use the trim() function to achieve this. The trim() function can remove blank characters or other predefined characters on both sides of a string and return the modified string, such as [trim($str)].

How to remove the first and last characters of a string in php

#If you want to remove characters on both sides of the string, you can use the PHP built-in function trim().

(Recommended tutorial: php video tutorial)

trim() function removes blank characters or other predefined characters on both sides of the string and returns the modified String.

Grammar format:

trim(string,charlist)

Example:

<?php
$str = " Hello World! ";
echo "Without trim: " . $str;
echo "<br>";
echo "With trim: " . trim($str);
?>

Related recommendations: php training

The above is the detailed content of How to remove the first and last characters of a string 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