Home  >  Article  >  Backend Development  >  How to remove the square brackets at both ends of a string in php

How to remove the square brackets at both ends of a string in php

青灯夜游
青灯夜游Original
2022-03-07 11:18:172856browse

php method to remove brackets at both ends of a string: 1. Use the ltrim() function to remove the brackets at the left end of the string; 2. Use the rtrim() function to remove the brackets at the right end of the string. The syntax is " ltrim(rtrim($str,"]"),"[")".

How to remove the square brackets at both ends of a string in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php removes strings The brackets at both ends

In PHP, you can use the ltrim() and rtrim() functions to remove the brackets at both ends of the string

  • The ltrim() function can remove the brackets on the left end of the string

  • The rtrim() function can remove the brackets on the right end of the string

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$str="[0jjhh45678fg0]";
echo ltrim($str,"[")."<br>";
echo rtrim($str,"]")."<br>";
echo ltrim(rtrim($str,"]"),"[");
?>

How to remove the square brackets at both ends of a string in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to remove the square brackets at both ends 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