tags:

views:

619

answers:

7

Hi, I have some articles stored in a text field in my database. There are times when the article includes <textarea>content.....</textarea> The problem is that when the content is displayed on a page using php, it includes the actual <br><br><br>. The <br> is placed there by the text editor (CKEditor) So it looks like the following

<textarea> <-- this actually becomes a textarea

This content is shown inside the text area<br> but the problem is that the <br> is also shown. </textarea>

How can I strip <br> from the <textarea> tags only. I imagine some preg replace would be useful.

+2  A: 

Your best bet is to use an HTML parser to do it:

$dom = new DOMDocument;
$dom->loadHTML($content);
foreach ($dom->getElementsByTagName('textarea') as $textarea) {
  foreach ($textarea->childNodes as $child) {
    if ($child->nodeName == 'br') {
      $textarea->removeChild($child);
    }
  }
}
$content = $dom->saveHTML();

Basically regexes are a poor tool for processing HTML because HTML isn't "regular" in the sense of "regular expression". This means it doesn't handle things like nested elements without using an unreliable hack.

cletus
Thank you very much. This works great for getting rid of the <br> but it seems to place the content outside of `<textarea></textarea>`Is there any way to keep the content inside the textarea? Thanks again.
mark
Actually the `<br />` are not removed, the content is just placed outside of the textarea instead.
mark
What do you mean they're getting placed outside the textarae?
cletus
despite reading hundreds of **"USE A HTML PARSER!!"** answers on Stack Overflow, this is actually the first one I've seen that actually gives some sample code. +1
nickf
@ cletus, When I use your code, the result is the following`<textarea></textarea>`The content is then removed from the textarea and placed under it. Not sure why.
mark
A: 

here's one way

$str= <<<A
    <textarea> <-- this actually becomes a textarea

    This content is shown inside the text area<br> but the problem is that the <br> is also shown. </textarea>
A;
$s = explode("</textarea>",$str);
for($i=0;$i<count($s)-1;$i++){
    if(strpos($s[$i],"<textarea>")!==FALSE){
        $s[$i] = preg_replace("/<br>/","",$s[$i]);
    }
}
print implode("</textarea>",$s);
ghostdog74
A: 

Have you had a look at what the database is storing - maybe it's HTMLencoding it before writing to the database and so what you're seeing is actually :

&lt;br /&gt;

This can happen when you use fields generated by CKEditor and TinyMCE as it escapes fields before $_POSTing

niggles
+2  A: 
$x='<br><textarea>abcd<br>efgh</textarea><br><br>';

echo preg_replace('/<textarea>.*?<\/textarea>/e','str_replace("<br>","","\\0")',$x);

//<br><textarea>abcdefgh</textarea><br><br>

If you using <br />, please update it appropriately.

S.Mark
This works great for `<textarea>`. How can I tweak it for instances where the textarea contains attributes.Example:`<textarea rows="10" name="test">Content with `<br>`</textarea>`Thanks.
mark
Like this: echo preg_replace('/<textarea[^>]*?>.*?<\/textarea>/e','str_replace("<br>","","\\0")',$x);
infamouse
Thanks infamouse for follow up, I missed to read his comment, and `?` may not needed in `<textarea[^>]*>`
S.Mark
A: 

can you notice that if you write a comment here and you missed the name and email..

even if you write a new line

it returns without with new line and without
or /r/n

can anyone told me how to do that ... because i have a similar form, first of all i saved all fields in variables and if one field is empty i reprint the values in the fields from the variables.

but unfortunately in textarea the value returned with /r/n or if i use the function to replace /r/n to
it print and new new lines printed

i appreciate your help and i envy this text area below :)

samer
A: 

finally i found the answer it is so strange.. but it fixed the problem.

as i notice.. if we write line 1 line2 line3

it actually write a the content with multi line and without
or or whatever

so i do the following function


function clear_string($string) { 
$string = str_replace(array('\r\n', '\r', '\n'), '
', $string); 
return $string; 
} 

and i use it whenever i want to save the content of textarea and save it on variable, also i can know easily echo the value of the variable for the textarea ... for example

<textarea><?php echo $notes;?> </textarea>

thank you so much for your help

and i hope it helps people who face same problem

samer
the answer is simple. you should not store these <br>'s at all, but add it dynamically before HTML output
Col. Shrapnel
A: 

Given the HTML...

<textarea id="text">Some text here.<br>Some other text.</textarea>

...perform this Javascript...

document.getElementById('text').value = document.getElementById('text').value.replace(/<br>/g,'\r');
dacracot