简体   繁体   English

PHP preg_match()正则表达式模式与空格匹配的问题

[英]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. 当使用preg_match()检查输入时,当用户键入类似于This comment基本上是在Firefox中找到了无效字符的匹配项,但是在Chrome中却没有,但是如果他们键入Thiscomment且在提交给服务器之前,则进行了preg_match check ,他们在单词之间添加空格就可以了。

Using pattern /^[a-zA-Z0-9 \\s\\.\\-]*$/ and have tried with only and without \\s and still no luck. 使用模式/^[a-zA-Z0-9 \\s\\.\\-]*$/并尝试仅使用\\ s和不使用\\ s仍然没有运气。

The content/text comes from a contentEditable table cell managed by Angular. 内容/文本来自Angular管理的contentEditable表单元格。

php 5.6 firefox/chrome latest angular 1.5 PHP 5.6 Firefox / Chrome最新角度1.5

A little more digging has shown that what is happening is firefox is inserting &lt;br&gt; 进一步的挖掘表明,正在发生的事情是firefox正在插入&lt;br&gt; , basically <br> , once in there deleting what has been added doesn't remove it. ,基本上是<br> ,一旦删除,添加的内容就不会删除。 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. 好的,更多的挖掘和实现工作我将需要弄清楚如何管理,因为chrome还会在向文本添加新行时插入字符,例如我希望支持的shift-return。 Will just have to manage how to store, \\n or &lt;br&gt; \\n&lt;br&gt;将只需要管理如何存储&lt;br&gt; or <br> AND manage when outputing to a text, alert, or table cell etc. <br>并在输出到文本,警报或表格单元等时进行管理。

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. Firefox有点古怪/激进,但并不是真的无关紧要,因为我是否要支持用户向文本中添加新行的功能,因此需要在所有浏览器中进行处理。

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. 解决方案是弄清楚如何将其管理为chrome,并且在任何浏览器中,当我要支持的新行(如shift-return)也将插入字符时。 Will just have to manage how to store, \\n or <br> or 只需管理存储方式,\\ n或<br>或
AND manage when outputing to a text, alert, or table cell etc. 输出到文本,警报或表格单元格等时进行AND管理。

So what I have done is use style="white-space: pre;" 所以我要做的是使用style="white-space: pre;" on the table cells and it renders \\n just like in an alert or textarea. 在表格单元格上,它呈现\\n就像在警报或文本区域中一样。 Got solution from angularjs newline filter with no other html angularjs换行过滤器获得解决方案,没有其他HTML

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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