I am dealing with a textarea user input which some characters are reserved for example:
[ means <h1>
] means </h1>
{ means <p>
} means </p>
Example user input:
[Hello World title]
{hello worlds paragraph
and paragraph continues after line break}
Example system display:
<h1>Hello World title<h1>
<p>hello worlds paragraph<br/>
and paragraph continues after line break</p>
All the HTML tags are filtered out from user input, and PHP script will replace these characters for the front-end.
I want to use PHP function “nl2br” to replace line breaks with <br/>.
The problem is only the line breaks between { and } characters should be replaced by <br />.
Also I need to consider if user input more than one paragraph using {}.
I’d do both replacements at the same time, using preg_replace_callback:
Test the code here
This example will add both the p tags and replace newlines with
in one single replacement.