Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8157007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:08:01+00:00 2026-06-06T17:08:01+00:00

I am dealing with a textarea user input which some characters are reserved for

  • 0

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 {}.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:08:03+00:00Added an answer on June 6, 2026 at 5:08 pm

    I’d do both replacements at the same time, using preg_replace_callback:

    <?php
    
    $input="{user input
    user input}
    
    newlines that aren't replaced";
    
    function format_paragraph($m){
        return '<p>'.nl2br($m[1]).'</p>';
    }
    
    echo preg_replace_callback("/\{(.*)\}/s",format_paragraph,$input);
    
    ?>
    

    Test the code here

    This example will add both the p tags and replace newlines with
    in one single replacement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Dealing with some legacy code and in trying to get a poorly designed database
Dealing with Java - which the option you prefer in the most cases -
When dealing with a single file, I'm used to: /blah do some work n
When dealing with big databases, which performs better: IN or OR in the SQL
I'm dealing with a class which extends JFrame . It's not my code and
I'm dealing with a null pointer exception for which I have a stack trace.
I dealing with the next problem, I have a asp.net web project in which
When dealing with any framework with which you are not 100% familiar, I've found
I'm new to dealing with cross-site scripting problems. We have some 404 pages that
I'm dealing with user subscriptions. A user creates a recurring subscription. Many things can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.