Home >Backend Development >PHP Problem >How to remove characters before and after a string in php
Removal method: 1. If the characters before and after are the same, you can use trim() to remove them. The syntax is "trim(string, "character")"; 2. If the characters before and after are not the same characters, you can use ltrim( ) and rtrim() to remove respectively, the syntax is "ltrim(rtrim(string,'last character'),'previous character')".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php removes strings Methods for characters before and after
1. If the characters before and after the string are the same, you can use the trim()
function to remove
trim(string,charlist)
<?php header('content-type:text/html;charset=utf-8'); $str = "a123456789a"; echo "原字符串:".$str; echo "<br>去掉字符后:".trim($str,"a"); ?>
2. If the characters before and after the string are not the same, you can use ltrim () and
rtrim() functions respectively remove
<?php header('content-type:text/html;charset=utf-8'); $str = "a123456789b"; echo "原字符串:".$str; echo "<br>去掉字符后:".ltrim(rtrim($str,'b'),'a'); ?>Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of How to remove characters before and after a string in php. For more information, please follow other related articles on the PHP Chinese website!