Broad Network


Wordwrap and Some Useful PHP String Features

PHP String with Security Considerations - Part 9

Foreword: In this part of the series I talk about the Wordwrap and Some Useful PHP String Features.

By: Chrysanthus Date Published: 29 Oct 2018

Introduction

This is part 9 of my series, PHP String with Security Considerations. In this part of the series I talk about the Wordwrap and Some Useful PHP String Features. You should have read the previous parts of the series before coming here, as this is the continuation.

Text Wrap
Assume that you have a paragraph of text, and there is no newline (n) character. You can break the paragraph into more lines using n. A line should be broken at a word boundary. A single string is returned with more lines. You should set the number of characters at or just before the number at which the line should be broken. The default line break separator is n, but you can also set (change) it.

To achieve all that, use the wordwrap() function, whose syntax is:

    string wordwrap ( string $str [, int $width = 75 [, string $break = "n" [, bool $cut = FALSE ]]] )

The function wraps a string to a given number of characters using a string break character. It returns the given string, wrapped to the specified length.

$str
    The input string.
$width
    The number of characters at which the string will be wrapped.
$break
    The line is broken using the optional break parameter; default is n.
$cut
    If the cut is set to TRUE, the string is always wrapped at or before the specified width. So if you have a word that goes beyond the given width, it is broken apart (see second example below). When FALSE the function does not split the word (even if the width is smaller than the word width).

Try the following code, which wraps the string to not more than 20 characters per line. The string break argument of rn is used.

<?php

    $str = 'The quick brown foxes jumped over the lazy dogs.';

    $ret = wordwrap ($str, 20, "rn");

    echo $ret;

?>

You have to use double quotes for the string break arguments and not single quotes. The output should be:

    The quick brown
    foxes jumped over
    the lazy dogs.

In the following example, a word is broken, because $cut is set to true.

<?php

    $str = 'A very long woooooooooooord.';

    $ret = wordwrap ($str, 8, "n", true);

    echo $ret;

?>

The output is:

    A very
    long
    wooooooo
    ooooord.

The echo Function
This function ouputs one or more strings. The syntax is:

    void echo ( string $arg1 [, string $... ] )

It returns void (empty).

It can be used without brackets, such as in:

    echo $ret, '<br>';

Here, '<br>' sends the cursor at the output to the next line below. With brackets, this statement would be:

    echo ($ret, '<br>');  //which did not work in my computer.

The arguments to the echo function are strings (numbers will become strings). You can also join its arguments with the dot operator, as in

    echo $ret . '<br>';

Try the following code:

<?php

    $str = 'I like PHP :)';

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

?>

The output is:

    I like PHP :)
    I like PHP :)

The number of arguments for the function can be more than two.

Here is a quotation from the PHP manual:

"echo is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo, the parameters must not be enclosed within parentheses."

That is it for this part of the series. We take a break 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