wrap text in php

php-sniplets Add comments

How to force text to wrap after a certain number of characters

A function that almost developers find really convenient is word-wrap. If you’ve a long string of text that carries
no specific formatting, you will be able to use word-wrap to insert a character, specified newline character (\n),
at a defined interval. word-wrap attends not to separate words unless you specific say it to. This function may
be specially valuable once it touches on building well-laid-out e-mail messages.

To utilize word-wrap, we barely pass it a string. word-wrap’s default behaviour is to enclose the text as just
about 75 characters as conceivable (it will not break words), entering a newline character (\n) at all breakpoint.

In this case, we mean to output HTML, so we provide 2 additional arguments to modify this default behaviour:

<?php $string = “Here is a long sentence that will be abbreviate at seventy
. characters automatically. Do not care,
. no words will follow broken up.”; echo wordwrap($string, 70, “<br />”); ?>

On this call, word-wrap encloses the text at 70 characters, and insets <br /> tags rather than new line
characters. Hera what it turnouts:

This is a long sentence that will be cut at seventy characters<br />
automatically. Do not care, none word will follow broken up.

Therefore, we have wrangled this clumsy sentence into something long more controllable without breaking
some of the words.

Leave a Reply

You must be logged in to post a comment.

Impressum Entries RSS Log in