replace <br /> tag inside textarea

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

replace <br /> tag inside textarea

Post by louie35 »

I need to replace the <br /> tag with chr(10) but only inside the <textarea>...</textarea> tag

Any ideas?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: replace <br /> tag inside textarea

Post 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>";
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Why? Do you use nl2br() when going INTO the database? You shouldn't.
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post 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.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Re: replace <br /> tag inside textarea

Post 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);
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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';
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

excellent. thanks guys.

i have been at this all morning.

where can I find an explanation of the above code?
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

http://www.php.net/manual/en/reference. ... syntax.php

especially the section "Assertions"
Post Reply