0

サイトに新しい機能があり、ユーザーはテキストエリアを介して任意のテキストを送信できます (すべての HTML エントリを停止しました)。私がまだ持っている主な問題は、彼らが「http://somewhere.com」と入力できることです。これは私が止めたいことです。また、特定の単語をブラックリストに登録したいと考えています。これは私が以前持っていたものです:

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

しかし、ユーザーがテキストをまったく送信できなくなったため、うまくいきませんでした。だから私の質問は簡単です、どうすればいいですか?

4

3 に答える 3

2

これは正規表現の仕事です。

あなたがそれをこのようにするために必要なこと:

// 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...");
}
于 2011-10-15T21:18:02.393 に答える
0

strpos を複数回使用する必要があります。あなたのやり方で or ステートメントを true / false で評価し、それを strpos に渡します。

このように動作するはずです:

if (strpos($entry, "http://") !== false || strpos($entry, "https://") !== false || strpos($entry, ".com") !== false)
于 2011-10-15T21:06:01.630 に答える
0

これを行う簡単な方法は、許可されていないすべての単語を配列に入れ、それらをループして各単語をチェックすることです。

$banned = array('http://', '.com', '.net', 'www.', '.org'); // Add more
foreach ($banned as $word):
    if (strpos($entry, $word) !== false) die('Contains banned word');
endforeach;

これの問題は、あまりにも夢中になって「com」などの単語を禁止し始めた場合、「com」という文字を含む完全に合法な単語やフレーズが他にもあり、誤検出を引き起こす可能性があることです. 正規表現を使用して URL のような文字列を検索することもできますが、上記のように簡単に分割することができます。人々がリンクをコメントに投稿するのを完全に止める効果的な方法はありません。それらをそこに置きたくない場合は、最終的には節度を使用する必要があります. コミュニティ モデレーションは非常にうまく機能します。たとえば、 Stack Overflowを見てください。

于 2011-10-15T21:12:01.637 に答える