PHP - accept break lines

3

I made a preg_match where it accepts several characters. At the moment I wanted to accept BREAK LINE also, when a person press ENTER to go down the line.

How do I do it?

Example of what I did:

!preg_match('#^[a-z0-9\-+, .\#*()\/]+$#i', $ads_description
    
asked by anonymous 27.04.2018 / 20:34

1 answer

3

It would probably be \n what you want, like this:

preg_match('#^[a-z0-9\-+, .\#*()\/\n]+$#i', $ads_description)

Of course you can use \s that will equal \n , space and tab ( \t ), so summarize the regular expression for:

preg_match('#^[a-z0-9\-+,.\#*()\/\s]+$#i', $ads_description)

Example in IDEONE: link

Just to note, a-z does not accept accents, only accepts a, e, i, o, u, b, c, d ..., letters like ã , õ , etc need to be specified, then it should look like this (to accept the letter a with accents, a short example just to understand):

preg_match('#^[a-z0-9ãáà\-+,.\#*()\/\s]+$#i', $ads_description)

But it is possible to simplify, using \p{L} (probably the data is in utf-8 then use the u modifier), like this:

<?php

$ads_description = '250.000km, Revisão feita.
Inspeccionado ate Julho 2018.
Novo kit embreagem, travoes, cardans. Encontra-se em muito bom estado.';

if (!preg_match('#^[\p{L}0-9\-+,.\#*()\/\s]+$#iu', $ads_description)) {
    echo 'Erro';
} else {
    echo 'Certo';
}

If underline / underscore _ can also be accepted then you could summarize a-z0-9 and _ with \w metachar (az is case insensitive, which would drop the i after # ), then this:

preg_match('#^[\w\-+,.\#*()\/\s]+$#', $ads_description)

It would be the same as:

preg_match('#^[a-z0-9\-+,.\#*()\/\s]+$#i', $ads_description)

    
27.04.2018 / 20:35