百科狗-知识改变命运!
--

ltrim() - 删除字符串开头的空白字符(或其他字符) - php 字符串函数

是丫丫呀1年前 (2023-11-21)阅读数 18#技术干货
文章标签字符

ltrim()

(PHP 4, PHP 5, PHP 7)

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

说明

ltrim(string $str[,string $character_mask] ): string

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

参数

$str

输入的字符串。

$character_mask

通过参数$character_mask,你也可以指定想要删除的字符,简单地列出你想要删除的所有字符即可。使用..,可以指定字符的范围。

返回值

该函数返回一个删除了$str最左边的空白字符的字符串。如果不使用第二个参数,ltrim()仅删除以下字符:

  • ""(ASCII32(0x20)),普通空白字符。
  • "t"(ASCII9(0x09)),制表符.
  • "n"(ASCII10(0x0A)),换行符。
  • "r"(ASCII13(0x0D)),回车符。
  • "0"(ASCII0(0x00)),NUL空字节符。
  • "x0B"(ASCII11(0x0B)),垂直制表符。

范例

ltrim() - 删除字符串开头的空白字符(或其他字符) - php 字符串函数

Example #1ltrim()的使用范例

以上例程会输出:

string(32) "        These are a few words :) ...  "
string(16) "    Example string
"
string(11) "Hello World"
string(30) "These are a few words :) ...  "
string(30) "These are a few words :) ...  "
string(7) "o World"
string(15) "Example string
"

参见

  • trim()去除字符串首尾处的空白字符(或者其他字符)
  • rtrim()删除字符串末端的空白字符(或者其他字符)
When using a $character_mask the trimming stops at the first character that is not on that mask.
So in the $string = "Hello world" example with $character_mask = "Hdle", ltrim($hello, $character_mask) goes like this:
1. Check H from "Hello world" => it is in the $character_mask, so remove it
2. Check e from "ello world" => it is in the $character_mask, so remove it
3. Check l from "llo world" => it is in the $character_mask, so remove it
4. Check l from "lo world" => it is in the $character_mask, so remove it
5. Check o from "o world" => it is NOT in the $character_mask, exit the function
Remaining string is "o world".
I hope it helps someone as I had a confusing moment with this function.
For those who use right-to-left languages such as Arabic, Hebrew, etc., it's worth mentioning that ltrim() (which stands for left trim) & rtrim() (which stands for right trim) DO NOT work contextually. The nomenclature is rather semantically incorrect. So in an RTL script, ltrim() will trim text from the right direction (i.e. beginning of RTL strings), and rtrim() will trim text from the left direction (i.e. end of RTL strings).
Guys, if += 0 is producing wrong values sometimes, and preg_replace is cpu consuming, then just stick to the main function described on that page, and use:

should be the fastest and most reliable.
I think all those comments can be misleading for begginers checking this page - it's sort of using magic tricks to reinvent the wheel.
@scion4581
The problem is that the character mask (_stw in your case) isn't a literal string, but a character collection. So all characters within _stw are stripped. In your case this includes the w of weight also.
+=0 not valid for something like 0000-5. Result is 0
To remove leading/trailing zeroes (example: "0123.4560"), doing a += 0 is easier than trim tricks.
I noticed one thing.
Suppose you have to ltrim zeroes from strings like "015", "002", etc. and convert them back to numbers, eg. using intval.
Suppose you must convert to number "000" too.
In this case:
$trimmedString = ltrim('000', '0');
the value of $trimmedString would be NULL (and I think the docs should mention this).
Anyway, I initially thought I had to conditional check for this '000', but I found that intval(NULL) returns 0 (this thing is not in the docs of intval but is in a comment)
What i found and i can't get why:
$word = 'stw_weight';
echo ltrim($word, 'stw_');
output: eight instead weight
If anybody know plz let me know
Keep in mind the amount of resources preg_replace() uses. 
I would suggest a simple if statement if you need to parse through large amounts of data. 

Code is untested, but probably sound.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)