PHP之string之ltrim()函数使用

本文详细介绍了PHP中的ltrim函数,该函数用于从字符串的开头移除空白字符或其他指定字符。文章通过实例展示了如何使用ltrim及其第二个参数来指定特定字符进行移除。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ltrim

  • (PHP 4, PHP 5, PHP 7)
  • ltrim — Strip whitespace (or other characters) from the beginning of a string
  • ltrim — 删除字符串开头的空白字符(或其他字符)

Description

string ltrim ( string $str [, string $character_mask ] )
//Strip whitespace (or other characters) from the beginning of a string.
//删除字符串开头的空白字符(或其他字符)

Parameters

str

  • The input string.
  • 输入的字符串。

character_mask

  • You can also specify the characters you want to strip, by means of the character_mask parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters.
  • 通过参数 character_mask,你也可以指定想要删除的字符,简单地列出你想要删除的所有字符即可。使用..,可以指定字符的范围。

Return Values

  • This function returns a string with whitespace stripped from the beginning of str. Without the second parameter, ltrim() will strip these characters:
  • 该函数返回一个删除了 str 最左边的空白字符的字符串。 如果不使用第二个参数, ltrim() 仅删除以下字符:
  • " " (ASCII 32 (0x20)), an ordinary space.普通空白字符。
  • "\t" (ASCII 9 (0x09)), a tab.制表符.
  • "\n" (ASCII 10 (0x0A)), a new line (line feed).换行符。
  • "\r" (ASCII 13 (0x0D)), a carriage return.回车符。
  • "\0" (ASCII 0 (0x00)), the NUL-byte.NUL空字节符。
  • "\x0B" (ASCII 11 (0x0B)), a vertical tab.垂直制表符。

Examples

<?php
/**
 * Created by PhpStorm.
 * User: zhangrongxiang
 * Date: 2018/3/4
 * Time: 下午4:37
 */

//ltrim — 删除字符串开头的空白字符(或其他字符)

$hello = "Hello World";
//Hello World
echo ltrim( $hello ) . PHP_EOL;
//ello World
echo ltrim( $hello, 'H' ) . PHP_EOL;
//llo World
echo ltrim( $hello, 'eH' ) . PHP_EOL;

$text = "\t\tThese are a few words :) ...  ";
//      These are a few words :) ...
echo $text . PHP_EOL;
//These are a few words :) ...
echo ltrim( $text ) . PHP_EOL;
//These are a few words :) ...
echo ltrim( $text, "\t" ) . PHP_EOL;
//      These are a few words :) ...
echo ltrim( $text, '\t' ) . PHP_EOL;

$binary = "\x09Example string\x0A";
//  Example string
//
echo $binary . PHP_EOL;
//Example string
//
echo ltrim( $binary ) . PHP_EOL;
//Example string
//
echo ltrim( $binary, "\x00..\x1F" ) . PHP_EOL;

See

转载于:https://www.cnblogs.com/zhangrxiang/p/8505116.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值