User Contributed Notes 9 notes

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 it2. Check e from "ello world" => it is in the $character_mask, so remove it3. Check l from "llo world" => it is in the $character_mask, so remove it4. Check l from "lo world" => it is in the $character_mask, so remove it5. 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:<?php$value = ltrim($value, '0');?>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.

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.