word-wrapping with PHP

A simple function to wrap text to a defined length using PHP. It takes a string as input, and returns a string with <br /> inserted to wrap the text on the page.

function word_wrap($text) {
    // Define the characters to display per row
    $chars = "10";
    $text = wordwrap($text, $chars, "<br />", 1);
    return $text;
}

Pretty self-explanatory really,  having defined the number of characters within your function, the input text is passed to the in-built PHP function along with the number of characters; then the wrapped text is passed back to you.

Advertisements

About Cameron
I'm a final year Computer Science/Information Systems major. Already finished my BA in Politics/Philosophy. I do web and software freelance on the side, while I finish studying. Hoping to be self-employed by the end of my degree, otherwise off into the real-world I go....

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: