search
Homephp教程php手册php trim() 函数实例讲解 - small_123

php trim() 函数移除字符串两侧的空白字符或其他预定义字符,本文章向码农介绍php trim() 函数的使用方法和实例,感兴趣的码农可以参考一下。

定义和用法

trim() 函数移除字符串两侧的空白字符或其他预定义字符。

相关函数:

  • ltrim() - 移除字符串左侧的空白字符或其他预定义字符
  • rtrim() - 移除字符串右侧的空白字符或其他预定义字符

 

语法

trim(string,charlist)

 

参数 描述
string 必需。规定要检查的字符串。
charlist

可选。规定从字符串中删除哪些字符。如果被省略,则移除以下所有字符:

  • "\0" - NULL
  • "\t" - 制表符
  • "\n" - 换行
  • "\x0B" - 垂直制表符
  • "\r" - 回车
  • " " - 空格

 

技术细节

返回值: 返回被修改的字符串
PHP 版本: 4+
更新日志: 在 PHP 4.1 中,新增了 charlist 参数。

 

实例

移除字符串两侧的空格:

<?php
$str = " Hello World! ";
echo "不使用 trim: " . $str;
echo "<br>";
echo "使用 trim: " . trim($str);
?>

在线运行

以上代码的 HTML 输出如下(请查看源代码):

<!DOCTYPE html>
<html>
<body>

不使用 trim:  Hello World! <br>使用 trim: Hello World!
</body>
</html>

以上代码的浏览器输出如下:

不使用 trim: Hello World!
使用 trim: Hello World!

原文地址:http://www.manongjc.com/article/827.html

相关阅读:

php trim() 函数移除字符串两侧的空白字符或其他预定义字符

php rtrim() 函数移除字符串右侧的空白字符或其他预定义字符

php trim() 函数移除字符串两侧的空白字符或其他预定义字符

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.