考虑:

$a = 'How are you?';

if ($a contains 'are')
    echo 'true';

假设我有上面的代码,如果($a包含“are”),写语句的正确方法是什么?


当前回答

Use:

$a = 'How are you?';
if (mb_strpos($a, 'are')) {
    echo 'true';
}

它执行多字节安全strpos()操作。

其他回答

您应该使用不区分大小写的格式,因此如果输入的值是小写或大写,则无所谓。

<?php
$grass = "This is pratik joshi";
$needle = "pratik";
if (stripos($grass,$needle) !== false) { 

 /*If i EXCLUDE : !== false then if string is found at 0th location, 
   still it will say STRING NOT FOUND as it will return '0' and it      
   will goto else and will say NOT Found though it is found at 0th location.*/
    echo 'Contains word';
}else{
    echo "does NOT contain word";
}
?>

在这里,stripos在不考虑箱子(小/帽)的情况下,在heystack中找到了针头。

带输出的PHPCode样本

如果要避免“虚假”和“真实”问题,可以使用subst_count:

if (substr_count($a, 'are') > 0) {
    echo "at least one 'are' is present!";
}

它比strpos慢一点,但它避免了比较问题。

许多使用subst_count的答案会检查结果是否大于0。但由于if语句将零视为false,因此可以避免该检查并直接写入:

if (substr_count($a, 'are')) {

若要检查是否不存在,请添加!操作员:

if (!substr_count($a, 'are')) {

如果搜索不区分大小写,则使用strstr()或stristr(。

参考SamGoody和Lego Stormtropr的评论。

如果您正在寻找基于多个单词的接近度/相关性对搜索结果进行排名的PHP算法这里有一种仅使用PHP生成搜索结果的快速简便方法:

其他布尔搜索方法(如strpos()、preg_match()、strstr()或stristr(

无法搜索多个单词结果未排名

基于向量空间模型和tf idf(术语频率–反向文档频率)的PHP方法:

这听起来很难,但却出奇地容易。

如果我们想搜索字符串中的多个单词,核心问题是如何为每个单词分配权重?

如果我们可以根据字符串作为一个整体的代表性来加权字符串中的项,我们可以按照与查询最匹配的结果排序。

这是向量空间模型的思想,与SQL全文搜索的工作原理相距不远:

function get_corpus_index($corpus = array(), $separator=' ') {

    $dictionary = array();

    $doc_count = array();

    foreach($corpus as $doc_id => $doc) {

        $terms = explode($separator, $doc);

        $doc_count[$doc_id] = count($terms);

        // tf–idf, short for term frequency–inverse document frequency, 
        // according to wikipedia is a numerical statistic that is intended to reflect 
        // how important a word is to a document in a corpus

        foreach($terms as $term) {

            if(!isset($dictionary[$term])) {

                $dictionary[$term] = array('document_frequency' => 0, 'postings' => array());
            }
            if(!isset($dictionary[$term]['postings'][$doc_id])) {

                $dictionary[$term]['document_frequency']++;

                $dictionary[$term]['postings'][$doc_id] = array('term_frequency' => 0);
            }

            $dictionary[$term]['postings'][$doc_id]['term_frequency']++;
        }

        //from http://phpir.com/simple-search-the-vector-space-model/

    }

    return array('doc_count' => $doc_count, 'dictionary' => $dictionary);
}

function get_similar_documents($query='', $corpus=array(), $separator=' '){

    $similar_documents=array();

    if($query!=''&&!empty($corpus)){

        $words=explode($separator,$query);

        $corpus=get_corpus_index($corpus, $separator);

        $doc_count=count($corpus['doc_count']);

        foreach($words as $word) {

            if(isset($corpus['dictionary'][$word])){

                $entry = $corpus['dictionary'][$word];


                foreach($entry['postings'] as $doc_id => $posting) {

                    //get term frequency–inverse document frequency
                    $score=$posting['term_frequency'] * log($doc_count + 1 / $entry['document_frequency'] + 1, 2);

                    if(isset($similar_documents[$doc_id])){

                        $similar_documents[$doc_id]+=$score;

                    }
                    else{

                        $similar_documents[$doc_id]=$score;

                    }
                }
            }
        }

        // length normalise
        foreach($similar_documents as $doc_id => $score) {

            $similar_documents[$doc_id] = $score/$corpus['doc_count'][$doc_id];

        }

        // sort from  high to low

        arsort($similar_documents);

    }   

    return $similar_documents;
}

案例1

$query = 'are';

$corpus = array(
    1 => 'How are you?',
);

$match_results=get_similar_documents($query,$corpus);
echo '<pre>';
    print_r($match_results);
echo '</pre>';

结果

Array
(
    [1] => 0.52832083357372
)

案例2

$query = 'are';

$corpus = array(
    1 => 'how are you today?',
    2 => 'how do you do',
    3 => 'here you are! how are you? Are we done yet?'
);

$match_results=get_similar_documents($query,$corpus);
echo '<pre>';
    print_r($match_results);
echo '</pre>';

结果

Array
(
    [1] => 0.54248125036058
    [3] => 0.21699250014423
)

案例3

$query = 'we are done';

$corpus = array(
    1 => 'how are you today?',
    2 => 'how do you do',
    3 => 'here you are! how are you? Are we done yet?'
);

$match_results=get_similar_documents($query,$corpus);
echo '<pre>';
    print_r($match_results);
echo '</pre>';

结果

Array
(
    [3] => 0.6813781191217
    [1] => 0.54248125036058
)

还有很多改进要做但是该模型提供了从自然查询获得良好结果的方法,它没有布尔运算符,例如strpos()、preg_match()、strstr()或stritr()。

不可接受的

可选地,在搜索单词之前消除冗余

从而减少索引大小并减少存储需求更少的磁盘I/O更快的索引和因此更快的搜索。

1.标准化

将所有文本转换为小写

2.停止字消除

从文本中删除没有实际意义的单词(如“and”、“or”、“the”、“for”等)

3.字典替换

将具有相同或相似含义的单词替换为其他单词。(例如:将“饥饿”和“饥饿”替换为“饥饿”)可以执行进一步的算法度量(滚雪球)以进一步将单词减少到其基本含义。用十六进制等价物替换颜色名称通过降低精度来减少数值是规范文本的其他方式。

资源

http://linuxgazette.net/164/sephton.htmlhttp://snowball.tartarus.org/MySQL全文搜索分数说明http://dev.mysql.com/doc/internals/en/full-text-search.htmlhttp://en.wikipedia.org/wiki/Vector_space_modelhttp://en.wikipedia.org/wiki/Tf%E2%80%93idfhttp://phpir.com/simple-search-the-vector-space-model/