+3 votes
in Programming Languages by (56.8k points)

I want to break a string into multiple lines. What PHP function should I use?

E.g.

"China has reported the world's first case of a human infected with H10N3 avian influenza in its eastern province of Jiangsu."

into 

"China has reported the world's

first case of a human infected with

H10N3 avian influenza in its

eastern province of Jiangsu"

1 Answer

+1 vote
by (349k points)
selected by
 
Best answer

You can use the wordwrap() function to break a string into multiple lines.

wordwrap(string,width,break,cut)

string = string you want to break

width = maximum line length. default is 75

break = default is "\n"

cut = whether words longer than the specified width should be wrapped. default is False.

 Here is an example:

<?php
$str = "China has reported the world's first case of a human infected with H10N3 avian influenza in its eastern province of Jiangsu.";
echo wordwrap($str,35);
?>

The above code will return the following output:

"China has reported the world's

first case of a human infected with

H10N3 avian influenza in its

eastern province of Jiangsu"


...