检测textarea提交中的特定单词 [英] Detecting specific words in a textarea submission

查看:173
本文介绍了检测textarea提交中的特定单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有一项新功能,用户可以通过textarea提交任何文本(我停止了所有HTML条目)。我仍然有一个主要问题,那就是他们可以输入http://somewhere.com,这是我想要阻止的事情。我也想黑名单特定的单词。这是我以前的样子:

  if(strpos($ entry,http://或.comor .net或www。或.org或.co.uk或https://)!== true){
die('条目不能包含链接!');

然而,这并不奏效,因为它阻止了用户提交任何文本。所以我的问题很简单,我该怎么做呢?

/www.google.co.uk/search?q=regular+expressionsrel =nofollow>正则表达式。



您需要做什么它是这样的:

  //你不允许的单词列表
$ disallowedWords = array(
'这些',
'字',
'是',
'不',
'允许'
);
//搜索不允许的单词。
//这里使用的正则表达式应该例如匹配'是',但不匹配'care'或'stare'
foreach($ disallowedWords as $ word){
if(preg_match(/ \ s + $ word \s + / i, $ entry)){
die(单词'$ word'不允许...);
}
}

//这个变量应该包含一个匹配URL的正则表达式
//这里有成千上万的数字,请选择。我只有
//使用了我在Google
$ urlRegex ='(http | https | ftp)\中找到的任意一个://([a-zA-Z0-9\.\\ \\ - ] +(。\:[A-ZA-Z0-9\&放大器;放大器;%\ $ \ - ] +)* @)*((25 [0-5] | 2 - [O- 4] [0-9] | [0-1] {1} [0-9] {2} | [1-9] {1} [0-9] {1} | [1-9])\ (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1-9] {1} [0-9 ] {1} | [1-9] | 0)\(25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] { 2} | [1-9] {1} [0-9] {1} | [1-9] | 0)\(25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1-9] {1} [0-9] {1} | [0-9])|本地主机|([A-ZA- Z0-9\ - ] + \)* [A-ZA-Z0-9\ - ] + \(COM |。EDU |政府网站|国际|万|网络|组织| BIZ | ARPA |资讯|名称|亲|航空|鸡舍|博物馆| [A-ZA-Z] {2}))(\:[0-9] +)*(/($ | [A-ZA-Z0-9\。 \?,\ \'\\\ +&放大器;放大器;%\ $#\ =〜_\ - ] +))*';

//搜索URL
if(preg_match($ urlRegex,$ entry)){
die(URLs not allowed ...);
}


I have a new feature on my site, where users can submit any text (I stopped all HTML entries) via a textarea. The main problem I still have though is that they could type "http://somewhere.com" which is something I want to stop. I also want to blacklist specific words. This is what I had before:

if (strpos($entry, "http://" or ".com" or ".net" or "www." or ".org" or ".co.uk" or "https://") !== true) {
            die ('Entries cannot contain links!');

However that didn't work, as it stopped users from submitting any text at all. So my question is simple, how can I do it?

解决方案

This is a job for Regular Expressions.

What you need to do it something like this:

// A list of words you don't allow
$disallowedWords = array(
  'these',
  'words',
  'are',
  'not',
  'allowed'
);
// Search for disallowed words.
// The Regex used here should e.g. match 'are', but not match 'care' or 'stare'
foreach ($disallowedWords as $word) {
  if (preg_match("/\s+$word\s+/i", $entry)) {
    die("The word '$word' is not allowed...");
  }
}

// This variable should contain a regex that will match URLs
// there are thousands out there, take your pick. I have just
// used an arbitrary one I found with Google
$urlRegex = '(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*';

// Search for URLs
if (preg_match($urlRegex, $entry)) {
  die("URLs are not allowed...");
}

这篇关于检测textarea提交中的特定单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆