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

  • SEARCH
  • Home
  • 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 7088777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:50:32+00:00 2026-05-28T07:50:32+00:00

I have a textarea on my page where i allow users to write text.

  • 0

I have a textarea on my page where i allow users to write text. To preserve linebreaks that users make while inputting I use:

editBox.val().replace(/\r?\n/g, "\r\n");

Before the data is uploaded to the database I’m using:

$data = mysql_real_escape_string($data);

I do this to preserve as much data as possible without stripping data that might be useful later on. This also helps me to preserve possible formatting options that can be allowed later. I read that this is good practice.

The problem is:

When the data is pulled from the database i need to clean it. For that I’m using:

function cleanData($data) 
{           
    $data = nl2br($data);
    $data = strip_tags($data,"<br><b><p><i><h1><h2><h3><h4><h5><h6>");
    return $data;
}

I’m allowing certain tags that will later be used with a little self-made wysiwyg editor.
However this allows for users to input the following:

<p title="some junk here">hax</p>

While the title attribute isn’t incredibly annoying, other attributes may. I’m not sure whether a user can add class and id attributes, but i can’t see why they shouldn’t be able to. It also removes anything that looks like a tag such as smilies: "*<:o) <- happy clown" would end up looking something like this: "*"

I tried using:

$data = filter_var($data, FILTER_SANITIZE_SPECIAL_CHARS);

Instead of the cleanData function, however this encodes everything showing my <br> converted line breaks as text instead of adding line breaks.

In short my problem is:

I can’t seem to find a nice way to clean the data so linebreaks/br are preserved while also retaining the possibillity to add some sort of wysiwyg formatting. I don’t really care whether it’s html tags or something like bbCode: [b].

My question is as follows:

Is there a smarter way to do this or is my method fine with a few tweaks?

What would you guys do? 🙂 I would like to avoid using external libraries unless there’s a very strong inscentive.

PS: I’ve searched around lot and found no satisfactory answer – I’ve also spent a long time making this post readable and understandable. I hope I’ve done it right.

  • 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-05-28T07:50:33+00:00Added an answer on May 28, 2026 at 7:50 am

    First of all,

    editBox.val().replace(/\r?\n/g, "\r\n");
    

    That should not be done on client side (JavaScript), but instead on server side (PHP) if you want to be certain that it happens. It can be circumvented by disabling JavaScript or posting from from another site.

    Regarding the actual question, I’d go with some pre-made markup language, such as Textile, which is also what’s being used here on StackOverflow.

    But if you wish to allow for a some custom formatting, you could (as you suggest yourself) use BB-code ([b], [i], etc.). The way I’d implement this is to first replace all HTML special characters with their respective HTML entities using htmlspecialchars. After this, you could replace things such as [b] with <strong>, etc.

    Example

    $str = "See, [b]evil[/b] input<br/>, <i>etc</i>.";
    $str = htmlspecialchars($str);
    print $str; // "See, [b]evil[/b] input &lt;br/&gt;, &lt;i&gt;etc&lt;/i&gt;."
    $str = str_replace(array("[b]","[/b]"),array("<b>","</b>",$str);
    print $str; // "See, <b>evil</b> input &lt;br/&gt;, &lt;i&gt;etc&lt;/i&gt;."
    

    To avoid bad markup, you should probably use some regular expressions to replace the BBcode with HTML tags.

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

Sidebar

Related Questions

I have a textarea within an HTML page into which my users paste content.
I have a user form with a textarea that allows users to submit html
I have a problem: I have a textarea on my page that looks like
On my page I have a textarea where users can enter a list of
I have a textarea that submits its value to a php page onkeypress. the
I have a page that users are able to add xml markup into a
I have a textarea html element on my page that gets reloaded via ajax.
I have a page with radio buttons and a textarea that populates data dynamically
I have a page that allows users to comment on answers very similar to
i have a textarea and I am using ckeditor to allow users to enter

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.