PHPを使用してテキストを検索する方法は?
何かのようなもの:
_<?php
$text = "Hello World!";
if ($text contains "World") {
echo "True";
}
?>
_
if ($text contains "World") {
を作業条件に置き換えることを除きます。
必要なのは strstr()
(またはstristr()
、LucaBが指摘したように)です。次のように使用します。
if(strstr($text, "world")) {/* do stuff */}
複数の単語の関連性に基づいて検索結果をランク付けするアルゴリズムを探している場合は、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>';
[〜#〜] results [〜#〜]
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>';
[〜#〜] results [〜#〜]
Array
(
[1] => 0.74864218272161
[2] => 0.43398500028846
)
これはあなたが探しているものかもしれません:
<?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
?>
以下のドキュメントでそれらに関するすべてを読むことができます:
私の意見では、strstr()はstrpos()よりも優れています。 strstr()はPHP 4 AND PHP 5.と同じですが、strpos()はPHP 5.サーバーの一部にはPHP 5
/* 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>
最善の解決策は私の方法です:
私の方法では、完全な単語のみが検出されますが、他の方法では検出されません。
例:$ text = 'hello world!'; if(strpos($ text、 'wor')=== FALSE){echo '"wor' not found in string '; }
結果:strposはtrueを返しました!!!しかし、私のメソッドではfalseを返します。
私の方法:
パブリック関数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;
}