PHP - accept break Lines

Asked

Viewed 43 times

3

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

How do I do?

Example of what I did:

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

1 answer

3

It would probably be \n what you want, so:

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

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

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

Example in IDEONE: https://ideone.com/wMIwLL

Just for the record, a-z does not accept accents, only accepts a,e,i,o,u,b,c,d..., letters like ã, õ, etc need to be specified, so 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 so use the modifier u), thus:

<?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';
}

Se underline/underscore _ can also be accepted, so you could summarize a-Z0-9 and _ with metacharacter \w (a-z is case insensitive, which would dispense with the modifier i after the #), so this:

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

It would be the same as:

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

  • I entered exactly the one that sent me, but it gives me an error when I insert this message "250.000km, Revision made. Inspected until July 2018. New kit clutch, brakes, cardans. It is in very good condition." will have introduced some character invalid?

  • whereas I am using if(! preg_match......)

  • @Rufus1090 the ! makes the denial, your intention is what? That "enter" the IF when you have right? Then you have to take the !, now if the intention is not to enter the IF then this right, please create an example in https://ideone.com work and sends me to see why you don’t join IF.

  • the intention is that only accept these characters. Hence the use of !

  • for example, if you don’t have these characters, it shows message.

  • example: if(!preg_match('# [ w-+,.#*() / s]+$#', $ads_description)) { $errors[] = "

  • @Rufus1090 puts an example in IDEONE. Here it seems this normal.

  • Yes the intention is the same, "IF THE TEXTAR FIELD CONTAINS OTHER CHARACTERS THAN THESE, IT OF THE ERROR"

  • I introduced this sentence and it accuses of error. "250.000km, Revision made. Inspected until July 2018. New clutch kit, brakes, cardans. It is in very good condition."

  • @Rufus1090 no use UPPERCASE EVERYTHING, the problem is that you haven’t posted the full code and I can’t read minds, let’s get down to business, here it works https://ideone.com/DKsj92 ---- If this does not work is due to another character that has nothing to do with line breaks, I have already spoken creates an example of the error in IDEONE and sends me: https://ideone.com if it will not be impossible to help you

  • this is https://ideone.com/7p2YZv

  • @Rufus1090 this is not a functional example of the problem, it’s just a piece of code pasted that does not run, I asked to create an example works of the problem, that is a summary example with the part that matters and that really works. All right forget it, I think I’ve already found the problem anyway and I’m editing the answer. Wait a few minutes

  • @Rufus1090 ready, I edited the answer.

  • K.. I understand the error, when I Preciono ENTER(line below), it simply activates the error.. That’s why it doesn’t let go.. s does not result at all... nor the

  • OK, is there any way to accept all characters of the type ()-*$%--- ?

  • @Rufus1090 put your script all for me to test please, send the http://pastebin.com and send me the link here

  • A question, what if I just wanted the user not to be able to put "<>, «», #, |, how would I do it? ?

  • @Rufus1090 like this: if (preg_match('#[<>«»|?]#', $ads_description)) { echo 'Não pode "<>, «», #, |, ?"'; } else { echo 'Funcionou'; }

Show 13 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.