Home > Article > Backend Development > How to remove the first and last string in php
Removal method: 1. Use the trim() function to delete substrings with the same beginning and end, the syntax is "trim($str,"specified substring")"; 2. Use the ltrim() and rtrim() functions To delete substrings with different beginning and end, the syntax is "ltrim(rtrim($str,"substring at the end"),"substring at the beginning")".
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
php remove the first and last characters String method
#Case 1: Delete strings with the same beginning and end--use trim()
trim() to delete both strings Specified substring on the side
<?php $str = 'abc12345abc'; echo $str . "<br>"; echo trim($str,"abc"); ?>
Case 2: Delete strings with different beginning and end--use ltrim() and rtrim()
ltrim() - Removes the specified substring on the left side of the string
rtrim() - Removes the specified substring on the right side of the string
<?php $str = 'ABC12345abc'; echo $str . "<br>"; echo ltrim(rtrim($str,"abc"),"ABC"); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove the first and last string in php. For more information, please follow other related articles on the PHP Chinese website!