Broad Network


Trimming and Padding Strings in PHP

PHP String with Security Considerations - Part 7

Foreword: In this part of the series I talk about Trimming and Padding Strings in PHP.

By: Chrysanthus Date Published: 29 Oct 2018

Introduction

This is part 7 of my series, PHP String with Security Considerations. In this part of the series I talk about Trimming and Padding Strings in PHP. You should have read the previous parts of the series before coming here, as this is the continuation.

Whitespace Characters
Here is a list of whitespace characters:

        Sequence     Note              Description
        t                 tab                 (HT, TAB)
        v                 vertical tab      (VT)
        n                newline         (NL)
        r                 return              (CR)
        f                 form feed         (FF)
        ' '                 space
        b                backspace         (BS)
        " "              NUL-byte

Trimming All Whitespaces from Beginning
You can strip all the whitespaces from the beginning of a string. The syntax is:

    string ltrim ( string $str )

The function retuns the string with all preceding whitespaces removed. The original string remains unchanged.

<?php

    $str = "tspare part";

    $ret = ltrim ($str);

    echo $str, '<br>';
    echo $ret;

?>

The output should be:

    spare part
spare part

where there is no tab (space) at the returned string.

Trimming All Whitespaces from the End
You can strip all the whitespaces from the end of a string. The syntax is:

    string rtrim ( string $str )

The function returns the string with all proceeding whitespaces removed. The original string remains unchanged.

<?php

    $str = "spare partn";

    $ret = rtrim ($str);

    echo $str, '<br>';
    echo $ret;

?>

The output is:

    spare part
    spare part

with no blank (new) line after the second print.

Trimming from Beginning and End
You can strip all the whitespaces from the beginning and from the end of a string using one function. The syntax is:

    string trim ( string $str )

The function returns the string with all preceding and proceeding whitespaces removed. The original string remains unchanged. Try the following code:

<?php

    $str = "tspare partn";

    $ret = trim ($str);

    echo $str, '<br>';
    echo $ret;

?>

The output is:

        spare part
    spare part

with no blank (new) line after the second print.

The ltrim(), rtrim() and trim() functions do not work with single quotes for the argument; they work with double quotes.

Padding a string with Blanks or Zeros
You can left pad a string with spaces or zeroes. You can right pad a string with spaces. You pad a string to obtain a particular string length. The syntax is:

    string str_pad ( string $input , int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT ]])

This function returns the input string padded on the left, or the right, or both sides to the specified padding length. If the optional argument $pad_string is not supplied, the input is padded with spaces, otherwise it is padded with characters from $pad_string up to the limit. The original string remains unchanged.

$input
    The input string.

$pad_length
    This is the new total length of the string. If the value of pad_length is negative, less than, or equal to the length of the input string, no padding takes place, and input will be returned.

$pad_string
        The string to pad, which may actually be more than 1 character long. The default is the single space character, ' '. (The pad_string may be truncated if the required number of padding characters can't be evenly divided by the pad_string's length.)

$pad_type
    Optional argument $pad_type; can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH. If pad_type is not specified it is assumed to be STR_PAD_RIGHT (default).

- STR_PAD_RIGHT means pad on the right.
- STR_PAD_LEFT means pad on the left.
- STR_PAD_BOTH means pad on both sides.

Try the following code, which right-pads the word, book, on the right with spaces, to have 7 characters

<?php

    $str = 'book';
    $pad_len = 7;

    $ret = str_pad ($str, $pad_len, ' ', STR_PAD_RIGHT);

    echo $str, '<br>';
    echo $ret;

?>

The output is:

book
"book   "

without the quotes, but with 3 padded spaces for the second line.

Try the following, which pads zeros on the left.

<?php

    $str = 2645;
    $pad_len = 8;

    $ret = str_pad ($str, $pad_len, 0, STR_PAD_LEFT);

    echo $str, '<br>';
    echo $ret;

?>

The output is:

2645
00002645

That is it for this part of the series. We stop here and continue in the next part.

Chrys


Related Links

Basics of PHP with Security Considerations
White Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links

Cousins

BACK NEXT

Comments