Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    341
    Thanks
    22
    Thanked 1 Time in 1 Post

    Replacing <br/> with newline

    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:
    Code:
    <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

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,492
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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 Code:
    <?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);
    }

    ?>

  • #3
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    341
    Thanks
    22
    Thanked 1 Time in 1 Post
    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:
    Code:
    		<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

  • #4
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    844
    Thanks
    15
    Thanked 9 Times in 9 Posts
    PHP Code:
    <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.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #5
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    341
    Thanks
    22
    Thanked 1 Time in 1 Post
    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
    Code:
    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:
    Code:
    str_replace("<br/>", "\n",...
    box one:
    Code:
    str_replace("\n", "<br/>",...
    This addressed the problem...

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •