PHP Dev
damienseguy's profile
Damien seguy@damienseguy•Apr 08
21.4K
Exakat's profile
Post cover image

How to trim strings in PHP with trim()

From benjamincrozat.com•Apr 08•5m read time

A practical guide to PHP's trim() function covering its signature, default behavior, and common use cases like cleaning form input, removing file line endings, and stripping path slashes. Key gotchas explained include: trim() only affects string edges (not the middle), it returns a new string rather than mutating the original, and its second argument is a character mask not an exact substring. Also covers Unicode whitespace limitations and the mb_trim() alternative introduced in PHP 8.4, plus comparisons with ltrim() and rtrim().

Sort:

damienseguy's user avatar
Damien seguy
@damienseguy
Joined Oct 25. 2023
21.4K
Exakat's profile

Exakat

Verified

PHP developer passionate about deep language knowledge, testing, static analysis, and sustainable it

Would you recommend this post?

Copy link
WhatsApp
Facebook
X
New Squad
  • © 2026 Daily Dev Ltd.
  • Guidelines
  • Explore
  • Tags
  • Sources
  • Squads
  • Leaderboard