Skip to Content
Programming PHP
book

Programming PHP

by Rasmus Lerdorf, Kevin Tatroe
March 2002
Intermediate to advanced
528 pages
21h 29m
English
O'Reilly Media, Inc.
Content preview from Programming PHP

Cleaning Strings

Often, the strings we get from files or users need to be cleaned up before we can use them. Two common problems with raw data are the presence of extraneous whitespace, and incorrect capitalization (uppercase versus lowercase).

Removing Whitespace

You can remove leading or trailing whitespace with the trim( ), ltrim( ), and rtrim( ) functions:

$trimmed = trim(string [, charlist ]);
$trimmed = ltrim(string [, charlist ]);
$trimmed = rtrim(string [, charlist ]);

trim( ) returns a copy of string with whitespace removed from the beginning and the end. ltrim( ) (the l is for left) does the same, but removes whitespace only from the start of the string. rtrim( ) (the r is for right) removes whitespace only from the end of the string. The optional charlist argument is a string that specifies all the characters to strip. The default characters to strip are given in Table 4-3.

Table 4-3. Default characters removed by trim( ), ltrim( ), and rtrim( )

Character

ASCII value

Meaning

" "

0x20

Space

"\t"

0x09

Tab

"\n"

0x0A

Newline (line feed)

"\r"

0x0D

Carriage return

"\0"

0x00

NUL-byte

"\x0B"

0x0B

Vertical tab

For example:

$title = "   Programming PHP  \n";
$str_1 = ltrim($title);                // $str_1 is "Programming PHP  \n"
$str_2 = rtrim($title);                // $str_2 is "   Programming PHP"
$str_3 = trim($title);                 // $str_3 is "Programming PHP"

Given a line of tab-separated data, use the charset argument to remove leading or trailing whitespace without deleting the tabs:

$record ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming PHP, 3rd Edition

Programming PHP, 3rd Edition

Rasmus Lerdorf, Kevin Tatroe, Peter MacIntyre
Programming PHP, 2nd Edition

Programming PHP, 2nd Edition

Rasmus Lerdorf, Kevin Tatroe, Peter MacIntyre
Clean Code in PHP

Clean Code in PHP

Carsten Windler, Alexandre Daubois

Publisher Resources

ISBN: 1565926102Supplemental ContentCatalog PageErrata