Trim space beginning of the string?

I have a tags and I use this to trim spaces after the comma

$tags = rtrim($tags, ", \t\n");

But how can I trim the space at the beginning of the string?

Looks like found the solution:

$tags = ltrim($tags);

If you want to replace the whitespace at the beginning and end of the string (like running ltrim and rtrim both), try:

$str = preg_replace('/^\s+|\s+$/gim', '', $str);

(I’m pretty sure… it’s been a while since I worked with PHP. That’s similar to how JS would be done.)



or… just [FPHP]trim[/FPHP] :wink:

ltrim and rtrim are specialized versions of trim, which does both.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.