0

i am writing a php code that displays text from a database.
how would i make the text auto break <br /> after a certain amount of chars?
please help asap. thanks

Edited by mike_2000_17: Fixed formatting

6
Contributors
7
Replies
9
Views
6 Years
Discussion Span
Last Post by nuttyniall
0

Hi
If its to format the output from the database would you not be better using css to wrap the text placed in a container??

You can wrap text using php using the following

<?php
$text = "The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 20, "<br />\n");

echo $newtext;
?>

N

0

Hi,
You can use the given code for auto break.It will automatically add br tag to the string.
<?php $string = nl2br($string); ?>

The above code will help you

Edited by rajvinoth: forget

0

Hi,
You can use the given code for auto break.It will automatically add br tag to the string.
<?php $string = nl2br($string); ?>

The above code will help you

YES I agree with you just simply call this function

[B]nl2br()[/B]

it will make line breaks on your record to a new line.

0

The last code i gave wraps nearest word to index 20 of the string
The following code will split the line and insert the break after the 20th character

<?php
$text = "lets have a look how many chars before this breaks the string and inputs a line break";
$newtext = chunk_split($text, 20, "<br />\n");
 echo $text."<br/>";
echo $newtext;
?>

OUTPUT

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>

<body


lets have a look how many chars before this breaks the string and inputs a line break<br/> 
lets have a look how<br /> 
 many chars before t<br /> 
his breaks the strin<br /> 
g and inputs a line <br /> 
break<br /> 

</body>

</html>

Edited by nuttyniall: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.