使用'preg_replace_callback'时,从Textarea显示新行段落

Ok, so i had the paragraphs displaying fine when pulled out of a database. Using CSS i had the line...

white-space: pre-line;

New lines for paragraphs were displaying great.

But then i added some code to make the first letter of every sentence capitalized. The code is...

$description = htmlentities(strip_tags($_POST['desc']), ENT_QUOTES);
$desc = preg_replace_callback('/([.!?])\s*(\w)/', function ($matches) {
    return strtoupper($matches[1] . ' ' . $matches[2]);
},  ucfirst(strtolower($description)));

So although it worked it now does not render the new lines for new paragraphs. In other words it is ignoring the CSS line above or something.

Any ideas??

It's not ignoring the CSS, there's no new line anymore.

See you are matching what you want right but you are replacing without the new line characters the first ([.!?]) and the second (\w) groups of the expression.

Change the expression to '/([.!?])(\s*)(\w)/' then the return statement to strtoupper($matches[1] . $matches[2] . $matches[3])

It should work.