<?php
// 使用 trim() 函数去掉字符串两端的空格
$string = " Hello World! ";
$trimmed_string = trim($string);
echo $trimmed_string; // 输出: Hello World!
// 使用 ltrim() 函数去掉字符串左侧的空格
$left_trimmed_string = ltrim($string);
echo $left_trimmed_string; // 输出: Hello World!
// 使用 rtrim() 函数去掉字符串右侧的空格
$right_trimmed_string = rtrim($string);
echo $right_trimmed_string; // 输出: Hello World!
// 使用 str_replace() 函数去掉字符串中所有的空格
$no_space_string = str_replace(' ', '', $string);
echo $no_space_string; // 输出: HelloWorld!
?>
trim()
:去掉字符串两端的空格。ltrim()
:去掉字符串左侧的空格。rtrim()
:去掉字符串右侧的空格。str_replace()
:可以用来去掉字符串中所有的空格(包括中间的空格)。上一篇:php 循环数组
下一篇:php 取整数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站