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

Recommended Answers

All 7 Replies

Can you give some example code for us to have an idea regarding your script?

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

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

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.

Simply use <br> tag in php code. For example, $myvar .= $Row.'<br>';

none of those are working

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>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.