1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Replacing a text with other.

Discussion in 'PHP' started by arpit13, Jul 26, 2010.

  1. #1
    hi,

    i was making a script and i wanted to insert some text to mysql database and use "\n" for line breaks.
    means that i want a coding to turn "\n" into <br /> tag
    can anyone help?

    Thanks.
    SEMrush
     
    arpit13, Jul 26, 2010 IP
    SEMrush
  2. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #2
    lukeg32, Jul 26, 2010 IP
  3. arpit13

    arpit13 Active Member

    Messages:
    285
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    78
    Digital Goods:
    1
    #3
    ok it gave a break but it prints \n which i don't want :(.
    never mind mind i will try something else.
     
    arpit13, Jul 26, 2010 IP
  4. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    One of the guys that commented on that php.net page created a function to do what you want.

    
    function mynl2br($text) { 
       return strtr($text, array("\r\n" => '<br />', "\r" => '<br />', "\n" => '<br />')); 
    } 
    
    PHP:
     
    Deacalion, Jul 26, 2010 IP
  5. arpit13

    arpit13 Active Member

    Messages:
    285
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    78
    Digital Goods:
    1
    #5
    ok i didn't read the full page.

    thanks i will try this as well.
     
    arpit13, Jul 26, 2010 IP
  6. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    str_replace("<br />", "\n", "here I am<br /> this is meeee<br />");
    PHP:
     
    xenon2010, Jul 27, 2010 IP
  7. arpit13

    arpit13 Active Member

    Messages:
    285
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    78
    Digital Goods:
    1
    #7
    i tried thic coding before but it didn't work.
     
    arpit13, Jul 27, 2010 IP
  8. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    it works you just need to add more replacing patterns.
    $arr = array("<br>","<BR>","<br/>","<br />", "<BR/>");
    str_replace($arr, "\n", "here I am<br> this is meeee<br>");
    
    PHP:
    it works fine with me..
     
    xenon2010, Jul 27, 2010 IP
  9. manzar

    manzar Peon

    Messages:
    111
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    i think this code solve your problems
    str_replace("<br />", "\n", "here I am<br /> this is meeee<br />");
     
    manzar, Jul 27, 2010 IP
  10. arpit13

    arpit13 Active Member

    Messages:
    285
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    78
    Digital Goods:
    1
    #10
    xenon i need to trun \n to <br /> not <br /> to \n.
    well i think if itell the coding help will be easier.
    str_replace('\n','<br />',$r2[news]);
    echo "<hr /><b>".$r2[title]."</b><br /><br />".$r2[news]."<br /><br /><b>By Admin</b> on <b>
    ".$r2[date]."</b><hr />";
    PHP:
    i will now try to echo news within quotes
     
    arpit13, Jul 27, 2010 IP
  11. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #11
    Use double quotes;

    $str = str_replace(array("\r\n", "\n", "\r"), "<br />", $str);
    PHP:
     
    danx10, Jul 27, 2010 IP
    Deacalion likes this.
  12. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Nice solution danx10.
     
    Deacalion, Jul 27, 2010 IP
  13. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #13
    if you want to convert \n to <br>
    just use nl2br("\n\n\n\n");
     
    xenon2010, Jul 29, 2010 IP
  14. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #14
    Does'nt work in all cases, furthermore I beleive only supports the \n newline character? and not the other OS based chars?
     
    danx10, Jul 29, 2010 IP