Notes

Note:
Possible gotcha: removing middle characters

Because trim() trims characters from the beginning and end of
a string, it may be confusing when characters are (or are not) removed from
the middle. trim('abc', 'bad') removes both 'a' and 'b' because it
trims 'a' thus moving 'b' to the beginning to also be trimmed. So, this is why it "works"
whereas trim('abc', 'b') seemingly does not.

See Also

ltrim() - Strip whitespace (or other characters) from the beginning of a string

rtrim() - Strip whitespace (or other characters) from the end of a string

str_replace() - Replace all occurrences of the search string with the replacement string

Standard trim() functions can be a problematic when come HTML entities. That's why i wrote "Super Trim" function what is used to handle with this problem and also you can choose is trimming from the begin, end or booth side of string.<?phpfunction strim($str,$charlist=" ",$option=0){ if(is_string($str)) {// Translate HTML entities$return = strtr($str, array_flip(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES)));// Remove multi whitespace$return = preg_replace("@\s+\s@Ui"," ",$return);// Choose trim optionswitch($option) {// Strip whitespace (and other characters) from the begin and end of stringdefault: case 0:$return = trim($return,$charlist); break;// Strip whitespace (and other characters) from the begin of string case 1:$return = ltrim($return,$charlist); break;// Strip whitespace (and other characters) from the end of string case 2:$return = rtrim($return,$charlist); break;

You can combine character ranges and individual characters in trim()'s second argument (ditto for ltrim and rtrim). All of the specified characters and ranges will be used concurrently (i.e., if a character on either end of the string matches any of the specified charaters or character ranges, it will be trimmed). The characters and character ranges can be in any order (except of course that the character ranges need to be specified in increasing order) and may overlap.E.g., trim any nongraphical non-ASCII character:trim($text,"\x7f..\xff\x0..\x1f");