How To Reformat a Paragraph of Text

Q

How To Reformat a Paragraph of Text? - PHP Script Tips - PHP Built-in Functions for Strings

✍: FYIcenter.com

A

You can wordwrap() reformat a paragraph of text by wrapping lines with a fixed length. Here is a PHP script on how to use wordwrap():

<?php
$string = "TRADING ON MARGIN POSES ADDITIONAL 
RISKS AND IS NOT SUITABLE FOR ALL 
        INVESTORS. 
A COMPLETE LIST OF THE RISKS ASSOCIATED WITH MARGIN TRADING IS
AVAILABLE IN THE MARGIN RISK DISCLOSURE DOCUMENT.";
$string = str_replace("\n", " ", $string);
$string = str_replace("\r", " ", $string);
print(wordwrap($string, 40)."\n");
?>

This script will print:

TRADING ON MARGIN POSES ADDITIONAL
RISKS AND IS NOT SUITABLE FOR ALL
    INVESTORS.   A COMPLETE LIST OF THE
RISKS ASSOCIATED WITH MARGIN TRADING IS
 AVAILABLE IN THE MARGIN RISK DISCLOSURE
DOCUMENT.

The result is not really good because of the extra space characters. You need to learn preg_replace() to replace them with a single space character.

2007-04-21, 4983👍, 0💬