Home arrow PHP arrow Strings: Trimming and Counting

Strings: Trimming and Counting

In this conclusion to a five-part series on strings and regular expressions in PHP, you'll learn about padding and stripping a string, trimming characters from a string, counting the characters in a string, and more. This article is excerpted from chapter nine of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).

TABLE OF CONTENTS:
  1. Strings: Trimming and Counting
  2. Counting Characters and Words
  3. Counting the Total Number of Words in a String
  4. Taking Advantage of PEAR: Validate_US
By: Apress Publishing
Rating: starstarstarstarstar / 1
July 15, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Padding and Stripping a String

For formatting reasons, you sometimes need to modify the string length via either padding or stripping characters. PHP provides a number of functions for doing so. This section examines many of the commonly used functions.

Trimming Characters from the Beginning of a String

Theltrim()function removes various characters from the beginning of a string, including white space, the horizontal tab (\t), newline (\n), carriage return (\r),NULL(\0), and vertical tab (\x0b). Its prototype follows:

string ltrim(string str [, string charlist])

You can designate other characters for removal by defining them in the optional parametercharlist.

Trimming Characters from the End of a String

Thertrim()function operates identically toltrim(), except that it removes the designated characters from the right side of a string. Its prototype follows:

string rtrim(string str [, string charlist])

Trimming Characters from Both Sides of a String

You can think of thetrim()function as a combination ofltrim()andrtrim(), except that it removes the designated characters from both sides of a string:

string trim(string str [, string charlist])

Padding a String

Thestr_pad()function pads a string with a specified number of characters. Its prototype follows:

string str_pad(string str, int length [, string pad_string [, int pad_type]])

If the optional parameterpad_stringis not defined,strwill be padded with blank spaces; otherwise, it will be padded with the character pattern specified bypad_string. By default, the string will be padded to the right; however, the optional parameterpad_typemay be assigned the valuesSTR_PAD_RIGHT,STR_PAD_LEFT, orSTR_PAD_BOTH, padding the string accordingly. This example shows how to pad a string usingstr_pad():

<?php
   
echo str_pad("Salad", 10)." is good.";
?>

This returns the following:

--------------------------------------------
Salad    is good.
--------------------------------------------

This example makes use ofstr_pad()ís optional parameters:

<?php
   
$header = "Log Report";
   
echo str_pad ($header, 20, "=+", STR_PAD_BOTH);
?>

This returns the following:

--------------------------------------------
=+=+=Log Report=+=+=
--------------------------------------------

Note thatstr_pad()truncates the pattern defined bypad_stringiflengthis reached before completing an entire repetition of the pattern.



 
 
>>> More PHP Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: