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.

RegExp new line problem

Discussion in 'PHP' started by bumbar, May 7, 2014.

  1. #1
    Hello,

    I have the following case:
    ^[a-zA-Z0-9]+$
    This work. Allows to enter only Latin characters. But when I insert a new line ( Enter ) in the textarea is obtained mistake.
    How to avoid this error when entering a new line with Enter?

    Thanks.
    SEMrush
     
    bumbar, May 7, 2014 IP
    SEMrush
  2. tylerman169

    tylerman169 Member

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    2
    Trophy Points:
    43
    #2
    Can't you just include the newline character in your regex like below:

    ^[a-zA-Z0-9\n]+$
    Code (markup):
     
    tylerman169, May 9, 2014 IP
  3. neroux

    neroux Active Member

    Messages:
    566
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Simply use the g modifier and preg_match_all().
     
    Last edited: May 12, 2014
    neroux, May 12, 2014 IP
  4. Jigney

    Jigney Active Member

    Messages:
    168
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    93
    #4
    I think you should try this below Code
    preg_replace("/[\n\r]/","<br />", $some_value);
     
    Jigney, May 22, 2014 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,474
    Likes Received:
    1,919
    Best Answers:
    247
    Trophy Points:
    515
    #5
    If it's a textarea, you'd want FULL whitespace preservation, would you not? The above also strips SPACES -- if you want carriage returns, you probably want spaces too.

    ^[\w\s]+$

    \s means all whitespace -- spaces, tabs and carriage return/linefeeds are matched. The \w is the same as a-zA-Z0-9 except it also includes underscore... which I usually would allow through as it's not usually a deal-breaker on anything.

    Basically the above is the same as:

    ^[a-zA-Z0-9_\r\n\t\v\f ]+$

    Just a hell of a lot easier to say... though I'll often allow those other control characters through, then compress [\t\v\f]+ to a single space with a replace.
     
    deathshadow, May 22, 2014 IP
  6. hip_hop_x

    hip_hop_x Active Member

    Messages:
    522
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #6
    hip_hop_x, May 24, 2014 IP