SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    replace \n to <br> ... HOW !

    hi ..

    i have textarea in admin face for dynamic content site, and there link for template, onclick will show the textarea value in pop-up window .

    it`s work fine, but i need to replace the \n to <br> !

    how can i do this with js, in php is
    PHP Code:
    ereg_replace("\n","<br>",$value); 
    so how ?


    thx for help .

  2. #2
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: replace \n to <br> ... HOW !

    Originally posted by Richi
    in php is
    PHP Code:
    ereg_replace("\n","<br>",$value); 
    In php you might as well use the built in function!

    PHP Code:
    $somevar=nl2br($somevar); 
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  3. #3
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok ..

    but how in js ??

    there are form, news form, i nees to view the news content in pop-up windows befor i submit it to the db, i do it with js, coz no way to do it with php , but i need to convert the \n to <br> , else i`ll get all the textarea in 1 line !

  4. #4
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this i rekon this will work

    PHP Code:


    function stringReplace (origionalStringfindtextreplaceText)
    {
        var 
    pos 0;
        var 
    len findtext.length;
        
    pos     origionalString.indexOf(findtext);

        while (
    pos != -1)
        {
            
    preString origionalString.substring (0pos);
            
    postString origionalString.substring (pos lenorigionalString.length);
            
    origional_string preString replaceText postString;
            
    pos origionalString indexOf (findtext);
        }
        
        return 
    origionalString;
    }


        var 
    origional_string = new String ('');
        var 
    find_string      = new String ('');
        var 
    replace_string   = new String ('');
        var 
    result_string    stringReplace (origional_stringfind_stringreplace_string

  5. #5
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i got js error !

    Error : expcted';'

    i put it after this line
    PHP Code:
    var result_string    stringReplace (origional_stringfind_stringreplace_string); 
    and i got the same error !

  6. #6
    SitePoint Wizard Bill Posters's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    1,523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Richi
    var result_string = stringReplace (origional_string, find_string, replace_string);
    just in case it makes any difference (I'm not personally sure whether it is part of the DOM or just a custom VAR), but origional > original.

    But then I'm not javascript savvy enough to spot if it's relevant in this instance and may be getting my wires crossed.

    Just a thought
    New Plastic Arts: Visual Communication | DesignateOnline

    Mate went to NY and all he got me was this lousy signature

  7. #7
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Bill Posters


    just in case it makes any difference (I'm not personally sure whether it is part of the DOM or just a custom VAR), but origional > original.

    But then I'm not javascript savvy enough to spot if it's relevant in this instance and may be getting my wires crossed.

    Just a thought
    No, because origional is used throughout... I think the problem is in this line:

    PHP Code:
    pos origionalString indexOf (findtext); 
    there should be a . between origionalString and indexof, like this:

    PHP Code:
    pos origionalString.indexOf(findtext); 
    Might be wrong tho
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  8. #8
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes it .


    but when i put, i got windows alert msg, that this script is coz slow, or something like that, and ask to run to abort it, if i run it all the browsers must killed else restart !

  9. #9
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yea im fixing that now 4 you

  10. #10
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this works definately as I desk checked it, hope thats better

    PHP Code:
    <html>

    <
    head>

    <
    title>Replace String</title>


    <
    script>

    <!--

        
        function 
    stringReplace(originalStringfindTextreplaceText)
        {

        
            var 
    pos 0;

            var 
    len findText.length;

            
    pos originalString.indexOf(findText);

        
            while (
    pos != -1)
            {

                
    preString originalString.substring(0pos);

                
    postString originalString.substring(pos lenoriginalString.length);

                
    originalString preString replaceText postString;

                
    pos originalString.indexOf(findText);

            }

            
            return 
    originalString;

        }

    //-->

    </script>

    </head>

    <body>


    <script>

    <!--



        var origString = new String('Richi');

        var findString = new String('ichi');

        var replaceString = new String('ichard');

        var resultString = stringReplace(origString, findString, replaceString)



        with (document)
        {
            write('<pre>')
            write('original string : ' + origString.bold()        + '<br>');

            write('searched        : ' + findString.bold()        + '<br>');

            write('replaced        : ' + replaceString.bold()    + '<br>');

            write('result          : ' + resultString.bold()        );
            write('</pre>')
        }

    //-->

    </script>


    </body>

    </html> 
    Last edited by Andrew-J2000; Apr 15, 2002 at 05:30.

  11. #11
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A regular expression will be faster than a loop. Try this:

    Code:
    function switchBreaks() {
      var taInput;
      var sNewString;
    
      taInput = yourForm.yourTextArea.value;
      sNewString = taInput.replace(/[\n\r]+/g,'<br>');
      alert(sNewString);
    }

  12. #12
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    great ... done .



    thx all guys !

  13. #13
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i've never been good with exp, just wondering if theres a few decent tutorials about it?

  14. #14
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've never gotten the hang of them. It is still trial and error everytime. Check out this link:

    http://www.webreference.com/js/column5/


Bookmarks

Posting Permissions

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