O'Reilly logo

Sams Teach Yourself PHP in 24 Hours, Third Edition by Matt Zandstra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Manipulating Strings

PHP provides many functions that transform a string argument, subtly or radically.

Cleaning Up a String with trim(), Itrim(), and strip_tags()

When you acquire text from the user or a file, you can’t always be sure that you haven’t also picked up white space at the beginning and end of your data. trim() shaves any white space characters, including newlines, tabs, and spaces, from both the start and end of a string. It accepts the string to be modified, returning the cleaned-up version:

$text = "\t\t\tlots of room to breathe";
$text = trim( $text );
print $text;
// prints "lots of room to breathe";

Of course, this might be more work than you require. You might want to keep white space at the beginning of a string but remove ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required