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

How to remove the first and last string in php

青灯夜游
青灯夜游Original
2022-05-31 17:40:282562browse

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")".

How to remove the first and last string in php

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 = &#39;abc12345abc&#39;;
echo $str . "<br>";
echo trim($str,"abc");
?>

How to remove the first and last string in php

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 = &#39;ABC12345abc&#39;;
echo $str . "<br>";
echo ltrim(rtrim($str,"abc"),"ABC");
?>

How to remove the first and last string in php

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!

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