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.

Simple preg_replace

Discussion in 'PHP' started by alphacooler, Oct 9, 2006.

  1. #1
    I've got a preg_replace that tosses <li></li>'s around every new line. I know it works on windows machines, but I know the code doesn't account for any other systems.

    $body = preg_replace("/\n/","</li><li>",$body);

    What should I change here?

    Thanks!
     
    alphacooler, Oct 9, 2006 IP
  2. Evoleto

    Evoleto Well-Known Member

    Messages:
    253
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Try:

    
    $body = preg_replace("/\\r\\n|\\n|\\r/","</li><li>", $body);
    
    PHP:
     
    Evoleto, Oct 9, 2006 IP
  3. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Unless you need variable embedding you should always use single quotes for strings, it saves on parsing time and you dont have to use complicated escape sequences like \\r\\n.
     
    penagate, Oct 9, 2006 IP
  4. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #4
    I think this is what he wants:

    
    $body = preg_replace("/^(.*)\\n/","</li>$1<li>",$body);
    
    PHP:
    Peace,
     
    Barti1987, Oct 9, 2006 IP
  5. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That definitely won't do what you're hoping... The <li> tags are backwards. In any case, the original code was replacing every newline with a closing tag and then an opening tag, I'm assuming <ul><li> is output before the regex and </li></ul> after it.
     
    exam, Oct 9, 2006 IP