2010-10-16 37 views
19

如何使用php搜索文本?

喜欢的东西:

<?php 

$text = "Hello World!"; 

if ($text contains "World") { 
    echo "True"; 
} 

?> 

除非工作条件更换if ($text contains "World") {

+0

您可能会发现['S($ STR) - >包含( '世界')'](https://github.com/delight-im/PHP-Str/blob/8fd0c608d5496d43adaa899642c1cce047e076dc/ src/Str.php#L93)和['s($ str) - > containsIgnoreCase('World')'](https://github.com/delight-im/PHP-Str/blob/8fd0c608d5496d43adaa899642c1cce047e076dc/src/Str .php#L105)很有帮助,在[这个独立的库](https://github.com/delight-im/PHP-Str)中找到。 – caw 2016-07-27 01:19:19

回答

37

你的情况,你可以只使用strpos(),或stripos()为不区分大小写:

if (stripos($text, "world") !== false) { 
    echo "True"; 
} 
+0

即使对于小号和大号大写字母也适用。 – AMB 2013-06-12 14:12:42

+0

完美的工作 – Vivek 2016-02-26 15:19:46

8

你需要的是strstr()(或stristr(),像LucaB指出)。使用这样的:

if(strstr($text, "world")) {/* do stuff */} 
+4

strpos或stripos更适合OP给出的用例 - strstr会去构造一个新字符串的麻烦,只有被扔掉... – 2010-10-16 20:30:47

+1

添加到Paul的评论中,从PHP手册[ ''strstr()'](http://www.php.net/manual/en/function.strstr.php):“如果你只想确定干草堆内是否出现特定的针,则使用更快,更少的内存密集函数'strpos()'代替。“ – BoltClock 2010-10-16 20:35:13

+0

实际上,这是一个不合理的微观优化,在给出原始示例的情况下对strstr使用strpos。返回的字符串会浪费,但基于“性能”决定进行*单个*文本搜索似乎并不合理。 – mario 2010-10-16 21:14:24

2

这可能是你在找什么:

<?php 

$text = 'This is a Simple text.'; 

// this echoes "is is a Simple text." because 'i' is matched first 
echo strpbrk($text, 'mi'); 

// this echoes "Simple text." because chars are case sensitive 
echo strpbrk($text, 'S'); 
?> 

是吗?

或者,也许这样的:

<?php 
$mystring = 'abc'; 
$findme   = 'a'; 
$pos = strpos($mystring, $findme); 

// Note our use of ===.  Simply == would not work as expected 
// because the position of 'a' was the 0th (first) character. 
if ($pos === false) { 
    echo "The string '$findme' was not found in the string '$mystring'"; 
} else { 
    echo "The string '$findme' was found in the string '$mystring'"; 
    echo " and exists at position $pos"; 
} 
?> 

甚至这个

<?php 
$email = '[email protected]'; 
$domain = strstr($email, '@'); 
echo $domain; // prints @example.com 

$user = strstr($email, '@', true); // As of PHP 5.3.0 
echo $user; // prints name 
?> 

可以将所有关于他们的阅读文档在这里:

http://php.net/manual/en/book.strings.php

1
在我看来的strstr

( )比strpos()更好。因为strstr()与PHP 4和PHP 5兼容,但strpos()只与PHP 5兼容。请注意,部分服务器没有PHP 5

+1

Uhm-PHP5于2004年首次发布。如果与PHP4的兼容性确实是一个问题,我建议您更换为不同的托管公司。 – Edward 2015-01-12 10:24:03

3

如果您正在寻找一种算法来对基于搜索结果关于多个词的相关性,这里提供了一种使用PHP生成搜索结果的快捷方法。

向量空间模型在PHP

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); 
     $doc_count=count($corpus['doc_count']); 

     foreach($words as $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 fro high to low 
     arsort($similar_documents); 
    } 
    return $similar_documents; 
} 

实现你的情况

$query = 'world'; 

$corpus = array(
    1 => 'hello world', 
); 

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

成绩

Array 
(
    [1] => 0.79248125036058 
) 

匹配多个叫板多个短语

$query = 'hello world'; 

$corpus = array(
    1 => 'hello world how are you today?', 
    2 => 'how do you do world', 
    3 => 'hello, 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.74864218272161 
    [2] => 0.43398500028846 
) 

How do I check if a string contains a specific word in PHP?

0
/* https://ideone.com/saBPIe */ 

    function search($search, $string) { 

    $pos = strpos($string, $search); 

    if ($pos === false) { 

     return "not found";  

    } else { 

     return "found in " . $pos; 

    }  

    } 

    echo search("world", "hello world"); 

嵌入PHP在线:

body, html, iframe { 
 
    width: 100% ; 
 
    height: 100% ; 
 
    overflow: hidden ; 
 
}
<iframe src="https://ideone.com/saBPIe" ></iframe>

+0

重塑车轮? – MJoraid 2017-07-24 16:38:14