在页面上查找禁止的单词而不在其他单词中查找

时间:2017-10-04 01:04:53

标签: php regex curl

我正在尝试将禁止的字词过滤器添加到网络代理中。 我不是在页面上搜索禁止的单词,而是在加载的页面中搜索禁止的单词。 我实际上并没有在页面内部(元标记,内容)寻找禁止的单词。

所以,如果我正在寻找" cock"这个词,那么" cockerel"不应该触发过滤器。

我刚刚测试了这段代码,是的,正如预期的那样,代码可以工作但是你可以猜到有很多cpu功能循环通过。页面加载的一刻,另一刻它变灰并显示页面加载时间过长的迹象。而这一切都在localhost上。现在,我可以想象我的虚拟主机会做什么! 所以现在,我们必须提出更好的解决方案。有任何想法吗 ? 怎么样我们没有让脚本检查所有被禁止的单词的加载页面?如果找到1个被禁止的单词并且已经发出一个已经发现禁止单词的回声以及页面上的位置,我们如何让脚本暂停? (元标签,身体内容等)。 任何代码建议?

这是我到目前为止所得到的:

<?php

/*
ERROR HANDLING
*/

// 1). $curl is going to be data type curl resource.
$curl = curl_init();

// 2). Set cURL options.
curl_setopt($curl, CURLOPT_URL, 'https://www.buzzfeed.com/mjs538/the-68-
words-you-cant-say-on-tv?utm_term=.xlN0R1Go89#.pbdl8dYm3X');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );

// 3). Run cURL (execute http request).
$result = curl_exec($curl);
$response = curl_getinfo( $curl );

if( $response['http_code'] == '200' )
    {
        //Set banned words.
        $banned_words = array("Prick","Dick","***");

        //Separate each words found on the cURL fetched page.
        $word = explode(" ", $result);

       //var_dump($word);

       for($i = 0; $i <= count($word); $i++)
       {
           foreach ($banned_words as $ban) 
           {
              if (strtolower($word[$i]) == strtolower($ban))
              {
                  echo "word: $word[$i]<br />";
                  echo "Match: $ban<br>";
           }
          else
           {
                 echo "word: $word[$i]<br />";
                 echo "No Match: $ban<br>";  
            }
         }
      }
   }  

// 4). Close cURL resource.
curl_close($curl);

我被告知这样做:

将页面加载到字符串中。 将preg_match与&#34;字边界&#34;在加载的字符串上并循环显示被禁止的单词。

Q1,如何将页面加载到字符串中? 但是,我不知道如何开始这个。因此,包括我在内的所有新手都会欣赏任何示例代码。 欢迎使用任何代码示例。

更新: 我更新了插入miknik代码的代码。它工作正常,直到我在cURL之前添加这一行: $ banned_words = array(&#34; Prick&#34;,&#34; Dick&#34;,&#34; ***&#34;);

这是更新:

<?php

/*
ERROR HANDLING
*/

// 1). Set banned words.
$banned_words = array("Prick","Dick","***");

// 2). $curl is going to be data type curl resource.
$curl = curl_init();

// 3). Set cURL options.
curl_setopt($curl, CURLOPT_URL, 'https://www.buzzfeed.com/mjs538/the-68-
words-
you-cant-say-on-tv?utm_term=.xlN0R1Go89#.pbdl8dYm3X');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );

// 4). Run cURL (execute http request).
$result = curl_exec($curl);
$response = curl_getinfo( $curl );

if($response['http_code'] == '200' )
     {
          $regex = '/\b';      // The beginning of the regex string syntax
          $regex .= implode('\b|\b', $banned_words);      // joins all the 
          banned words to the string with correct regex syntax
          $regex .= '\b/i';    // Adds ending to regex syntax. Final i makes 
          it case insensitive
          $substitute = '****';
          $cleanresult = preg_replace($regex, $substitute, $result);
          echo $cleanresult;
     }

  curl_close($curl);

  ?>

1 个答案:

答案 0 :(得分:0)

您已将页面内容作为字符串,它位于$result

preg_match会有效,但是当你找到匹配项时,你想做什么?如果您想过滤禁止的字词,preg_replace更合适。

没有必要将字符串分解为单个单词,您只是通过这样做添加了大量的cpu开销。按原样处理$result变量。

首先,从你禁止的单词数组中构造一个正则表达式字符串。匹配每个单词的基本语法是\bXXXX\b,其中XXXX是您禁用的单词。每一端的\b表示它必须位于单词边界,因此\bcock\b会匹配公鸡和公鸡!但不是公鸡。

$regex = '/\b';      // The beginning of the regex string syntax
$regex .= implode('\b|\b', $banned_words);      // joins all the banned words to the string with correct regex syntax
$regex .= '\b/i';    // Adds ending to regex syntax. Final i makes it case insensitive

现在,您可以在$result上运行单个操作,并获取一个新字符串,其中包含所有被禁止的单词。设置您的值以替换每个被禁止的单词

$substitute = '****';

然后执行替换

$cleanresult = preg_replace($regex, $substitute, $result);

假设$result = 'You are a cock! You prick! You are such a dick.';

echo $cleanresult返回你是****!你****!你真是个****。