如何使用 php 搜索文本 if ($text contains "World")
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3950622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to search text using php if ($text contains "World")
提问by faressoft
How to search text using php?
如何使用php搜索文本?
Something like:
就像是:
<?php
$text = "Hello World!";
if ($text contains "World") {
echo "True";
}
?>
Except replacing if ($text contains "World") {
with a working condition.
除非更换if ($text contains "World") {
为工作状态。
回答by BoltClock
回答by Gabi Purcaru
回答by RafaSashi
If you are looking an algorithm to rank search results based on relevance of multiple words here comes a quick and easy way of generating search results with PHP only.
如果您正在寻找一种基于多个词的相关性对搜索结果进行排名的算法,这里提供了一种仅使用 PHP 生成搜索结果的快速简便方法。
Implementation of the vector space model in 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;
}
IN YOUR CASE
在你的情况下
$query = 'world';
$corpus = array(
1 => 'hello world',
);
$match_results=get_similar_documents($query,$corpus);
echo '<pre>';
print_r($match_results);
echo '</pre>';
RESULTS
结果
Array
(
[1] => 0.79248125036058
)
MATCHING MULTIPLE WORDS AGAINST MULTIPLE PHRASES
针对多个短语匹配多个单词
$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>';
RESULTS
结果
Array
(
[1] => 0.74864218272161
[2] => 0.43398500028846
)
from How do I check if a string contains a specific word in PHP?
回答by Trufa
This might be what you are looking for:
这可能是您正在寻找的:
<?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');
?>
Is it?
是吗?
Or maybe this:
或者也许这个:
<?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";
}
?>
Or even this
甚至这个
<?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
?>
You can read all about them in the documentation here:
您可以在此处的文档中阅读有关它们的所有信息:
回答by Mahdi Jazini
in my opinion strstr() is better than strpos(). because strstr() is compatible with both PHP 4 AND PHP 5. but strpos() is only compatible with PHP 5. please note that part of servers have no PHP 5
在我看来 strstr() 比 strpos() 更好。因为 strstr() 与 PHP 4 和 PHP 5 兼容。但 strpos() 仅与 PHP 5 兼容。请注意部分服务器没有 PHP 5
回答by antelove
/* 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");
Embed PHP online:
在线嵌入 PHP:
body, html, iframe {
width: 100% ;
height: 100% ;
overflow: hidden ;
}
<iframe src="https://ideone.com/saBPIe" ></iframe>
回答by M.ghorbani
The best solution is my method:
最好的解决方案是我的方法:
In my method, only full words are detected,But in other ways it is not.
在我的方法中,只检测到完整的单词,但在其他方式中则不然。
for example:
例如:
$text='hello world!';
if(strpos($text, 'wor') === FALSE) {
echo '"wor" not found in string';
}
Result: strpos returned true
!!! but in my method return false
.
结果:strpos 返回true
!!!但在我的方法中 return false
。
My method:
我的方法:
public function searchInLine($txt,$word){
$txt=strtolower($txt);
$word=strtolower($word);
$word_length=strlen($word);
$string_length=strlen($txt);
if(strpos($txt,$word)!==false){
$indx=strpos($txt,$word);
$last_word=$indx+$word_length;
if($indx==0){
if(strpos($txt,$word." ")!==false){
return true;
}
if(strpos($txt,$word.".")!==false){
return true;
}
if(strpos($txt,$word.",")!==false){
return true;
}
if(strpos($txt,$word."?")!==false){
return true;
}
if(strpos($txt,$word."!")!==false){
return true;
}
}else if($last_word==$string_length){
if(strpos($txt," ".$word)!==false){
return true;
}
if(strpos($txt,".".$word)!==false){
return true;
}
if(strpos($txt,",".$word)!==false){
return true;
}
if(strpos($txt,"?".$word)!==false){
return true;
}
if(strpos($txt,"!".$word)!==false){
return true;
}
}else{
if(strpos($txt," ".$word." ")!==false){
return true;
}
if(strpos($txt," ".$word.".")!==false){
return true;
}
if(strpos($txt," ".$word.",")!==false){
return true;
}
if(strpos($txt," ".$word."!")!==false){
return true;
}
if(strpos($txt," ".$word."?")!==false){
return true;
}
}
}
return false;
}