Looking for somewhere to host your site? Browse our PHP web hosts section!

Home > Code Snippets > Word wrap

Word wrap

Description

A function that takes a string of text and wraps it into lines of a length that you determine. Can be useful for guestbooks, news posting scripts etc. to prevent the layout breaking.

The code


<?php


/**
 * Example usage:
 * 
 * // Your text
 * $text = "This is a sentence which contains some words.";
 * 
 * // Or from a database result
 * $text = $row['text'];
 * 
 * // Then put it into the function
 * $text = word_wrap($text);
 * 
 * // Output the result
 * echo $text;
 */

    
function word_wrap($text) {

        
// Define the characters to display per row
        
$chars "10";

        
$text wordwrap($text$chars"<br />"1);

        return 
$text;

    }

?> 

Get the code

Download the file to your computer: Click here to get the file