...

View Full Version : Replacing <br/> with newline



mbarandao
11-12-2010, 05:13 PM
Good day all:

I have a textarea box in my form which receives (from a url) and display variable. The problem is that it does not translate the <br/>. instead of creating a newline where there is <br/>, it simply prints <br/>. here is an example of the output.

br/>On 11/11/10 @ 01:34<br/>We performed the following:<br/><br/>
information update<br/><br/>----End of Service Report----<br/><br/>On 11/11/10 @ 10:45<br/>We performed the following:<br/><br/>

Inside my form, the textarea is written as follows:


<td><textarea name="text" id="text" rows="26" cols="61" ><?php echo str_replace('<br/>', "\n", "$edit_servicedesc2");?></textarea>&nbsp;</td>


on other instances, the output is without the <br/>, instead, it removes the <br/> and does not replace it with newline. Something like this:

On 11/12/10 @ 11:47We performed the following:test1test2test3----End of Service Report----On 11/12/10 @ 11:24We performed the following:Information updatedOil Change performedBrake pads installed----End of Service Report...

Does anyone have any thoughts on how to resolve this issue?

Thanks
Mossa

mlseim
11-12-2010, 05:27 PM
There is already a built-in PHP function to go from New Line to BR:

$newtext = nl2br($string);


And here is a function to go from BR to New Line.
Probably other ways to do this, but give this a shot ...



<?php

$string = "On 11/11/10 @ 01:34<br/>We performed the following:<br/><br/>
information update<br/><br/>----End of Service Report----<br/><br/>On 11/11/10 @ 10:45<br/>We performed the following:<br/><br/>";

$newtext=br2nl($string);
echo $newtext;

function br2nl( $input ) {
return preg_replace('/<br(\s+)?\/?>/i', "\n", $input);
}

?>

mbarandao
11-12-2010, 05:57 PM
mlseim, thanks for the reply. I need a bit of clarification, if you don't mind --as much of this is new to me.

Now, since my textarea box is receiving this variable from a url in the following manner:


<td><textarea name="text" id="text" rows="26" cols="61" ><?php echo str_replace('<br/>', "\n", "$edit_servicedesc2");?></textarea>&nbsp;</td>



Where would I implement your suggestion?

Note, the information displayed in your post as the equal of $string is represented in the form --with the textarea box, as a variable. So would the value of $string=$edit_servicedesc2? or Do I have an incorrect understanding?

This may read a bit ambiguous, if so, I'll be happy to post my entire code with the hope to provide a better understanding.

Please advise...
Mossa

dniwebdesign
11-12-2010, 06:48 PM
<td><textarea name="text" id="text" rows="26" cols="61" ><?php
function br2nl( $input ) {
return preg_replace('/<br(\s+)?\/?>/i', "\n", $input);
}


echo br2nl($edit_servicedesc2);?></textarea>&nbsp;</td>
You would replace the str_replace for the br2nl.

mbarandao
11-12-2010, 10:42 PM
Thank you all, problem resolved.
My process may seem a bit complicated to explain as to how the issue was resolved, but let me give it shot--so that if someone else is attempt to achieve similar outcome.

I was attempting to pass variable via url. Some of the variables being passed, contained a textarea box with values retireved from db text field and displayed in the page.

The challenge was in part because I was passing the variables to multiple pages through two different link buttons.

Using the
str_replace("<br/>", "\n", cause a wierd effect whereas; on page, the <br/> was removed and was not replaced with a newline; instead it combined everything together--like this:

On 11/12/10 @ 11:47We performed the following:test1test2test3----End of Service Report----On 11/12/10 @ 11:24We performed the following:Information updatedOil Change performedBrake pads installed----End of Service Report...


On the other page, The problem was that it did not translate the <br/>. instead of creating a newline where there is <br/>, it simply prints <br/>. something like this:

br/>On 11/11/10 @ 01:34<br/>We performed the following:<br/><br/>
information update<br/><br/>----End of Service Report----<br/><br/>On 11/11/10 @ 10:45<br/>We performed the following:<br/><br/>


The solution I determined was:

On the originating --variable passing via url-- page, I created two functions with the str_replace strings. I created two textarea boxes both receiving the same values from db (as explained above) and however, made one box hidden. In each respective box, I added the str_replace function with the following modifications:

box one:

str_replace("<br/>", "\n",...


box one:

str_replace("\n", "<br/>",...


This addressed the problem...

Thanks for all of the post in response to this help request.
Mossa



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum