Page 1 of 1

replace <br /> tag inside textarea

Posted: Thu Aug 09, 2007 6:12 am
by louie35
I need to replace the <br /> tag with chr(10) but only inside the <textarea>...</textarea> tag

Any ideas?

Re: replace <br /> tag inside textarea

Posted: Thu Aug 09, 2007 6:17 am
by superdezign
louie35 wrote:I need to replace the <br /> tag with chr(10) but only inside the <textarea>...</textarea> tag

Any ideas?

Code: Select all

$data = preg_replace('@(<|>)br\s*/?(>|<)@', "\n", $data);
echo "<textarea>$data</textarea>";

Posted: Thu Aug 09, 2007 6:24 am
by louie35
Thanks for the reply but is not what i needed - sorry my bad

i have the data in the db and some is enclosed between the

Code: Select all

..
which i replace with <textarea ..>...</textarea>

after this i need to replace the <br /> tags with chr(10) but only whats inside the <textarea>..</textarea> tags

Posted: Thu Aug 09, 2007 6:26 am
by superdezign
Why? Do you use nl2br() when going INTO the database? You shouldn't.

Posted: Thu Aug 09, 2007 6:30 am
by louie35
no i don't but that's the way it goes in at the moment and need to do the replacement but only inside the textarea.

Re: replace <br /> tag inside textarea

Posted: Thu Aug 09, 2007 6:30 am
by stereofrog
louie35 wrote:I need to replace the <br /> tag with chr(10) but only inside the <textarea>...</textarea> tag

Any ideas?

Code: Select all

$re = <<<RE
~
	<br>
	(?=
		(
			(?<! <textarea) 
			.
		)*
		</textarea>
	)
~six
RE;

$test = "
keep this <br> and this <br>
remove <textarea name=foo><br> and this <bR> and that <Br></textarea>
keep <br> and <BR>
";

echo preg_replace($re, "REPLACED", $test);

Posted: Thu Aug 09, 2007 6:40 am
by louie35
how that's that work cause it gives me an error:

Code: Select all

syntax error, unexpected T_SL
haven't seen this type of coding before.

Posted: Thu Aug 09, 2007 6:48 am
by volka
louie35 wrote:syntax error, unexpected T_SL
the token T_SL stands for <<
But stereofrog wrote <<< which is a T_START_HEREDOC

One suggestion: use <br\s*/?> instead of <br> in the expression and it will also match <br />

If you can't get the heredoc syntax to work try

Code: Select all

$re = '~
      <br\s*/?>
        (?=
                (
                        (?<! <textarea)
                        .
                )*
                </textarea>
        )
~six';

Posted: Thu Aug 09, 2007 6:59 am
by louie35
excellent. thanks guys.

i have been at this all morning.

where can I find an explanation of the above code?

Posted: Thu Aug 09, 2007 7:04 am
by stereofrog
http://www.php.net/manual/en/reference. ... syntax.php

especially the section "Assertions"