简体   繁体   中英

PHP preg_match() regular expression pattern match issues with whitespace

When checking input using preg_match() I am getting a match for invalid character in Firefox but not Chrome when user types in something like This comment basically something with a space, but if they type in Thiscomment and before submitting to server, and preg_match check , they add a space between the words it will be fine.

Using pattern /^[a-zA-Z0-9 \\s\\.\\-]*$/ and have tried with only and without \\s and still no luck.

The content/text comes from a contentEditable table cell managed by Angular.

php 5.6 firefox/chrome latest angular 1.5

A little more digging has shown that what is happening is firefox is inserting &lt;br&gt; , basically <br> , once in there deleting what has been added doesn't remove it. So I am experimenting with striping it out before running thru regex validation.

Ok, so more digging and realizing I will need to figure out how to manage this as chrome will also insert the characters when a new line is added to the text, like shift-return, which I want to support. Will just have to manage how to store, \\n or &lt;br&gt; or <br> AND manage when outputing to a text, alert, or table cell etc.

Thanks to everyone who took a look and commented. Sorry for the noise.

I'm gonna close this ticket now, or delete if I can't close, would like to not delete to save someone else time with this but not sure how to do this.

So the basic, and now clearly obvious, issue is that the browser needs to insert something to handle line breaks, new lines, to support that feature for input. Firefox is a bit odd/aggressive with it but, doesn't really matter as if I want to support ability for users to add new lines to the text it will need to be handled from all browsers.

Solution is to figure out how to manage this as chrome, and any browser, will also insert the characters when a new line is added to the text, like shift-return, which I want to support. Will just have to manage how to store, \\n or <br> or
AND manage when outputing to a text, alert, or table cell etc.

So what I have done is use style="white-space: pre;" on the table cells and it renders \\n just like in an alert or textarea. Got solution from angularjs newline filter with no other html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM