检查STRING是否包含来自txt文件的术语(单词/单词) - PHP

时间:2017-03-19 13:47:28

标签: php string

我想知道STRING是否包含文本列表中的特定单词/单词(TXT FILE)。 该函数将在TXT文件中逐行查看,看看整个该行是否出现在STRING中。

<?php
function hebstrrev($string, $revInt = false, $encoding = 'UTF-8'){
    $mb_strrev = function($str) use ($encoding){return mb_convert_encoding(strrev(mb_convert_encoding($str, 'UTF-16BE', $encoding)), $encoding, 'UTF-16LE');};

    if(!$revInt){
      $s = '';
      foreach(array_reverse(preg_split('/(?<=\D)(?=\d)|\d+\K/', $string)) as $val){
        $s .= ctype_digit($val) ? $val : $mb_strrev($val);
      }
      return $s;
    } else {
      return $mb_strrev($string);
    }
  }

function is_rtl( $string ) {
    $rtl_chars_pattern = '/[\x{0590}-\x{05ff}\x{0600}-\x{06ff}]/u';
    return preg_match($rtl_chars_pattern, $string);
}

header('Content-Type: text/html; charset=UTF-8');

$productFile = file_get_contents('vlist.txt');
mb_convert_encoding($productFile, 'UTF-16LE', 'UTF-8');
$products = str_word_count($productFile, 1);

$text = 'השבוע נתניה - מגזין';
$text = iconv(mb_detect_encoding($text, mb_detect_order(), true), "UTF-8", $text);

$found = false;
foreach ($products as $product)
{
    if(is_rtl($product)) $product = hebstrrev($product, true); 
    $product = iconv(mb_detect_encoding($product, mb_detect_order(), true), "UTF-8", $product);

    if (mb_strpos($text,$product) !== false) {
        $found = true;
        break;
    }
}

if ($found) {
    echo 'the status contains a product';
}
else {
    echo 'The status doesnt contain a product';
}

问题是该功能正在逐字检查STRING。不检查每条线是否部分出现在STRING中。

0 个答案:

没有答案