Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用文本字段进行搜索?php_Php - Fatal编程技术网

如何使用文本字段进行搜索?php

如何使用文本字段进行搜索?php,php,Php,我想通过在文本字段中插入一个单词来搜索整个新闻提要。我不知道怎么做。这是我的代码,所以你知道我的意思 <form action="search.php" method="get"> <tr><th>search: </th><td><input type="text" name="search" value="{$word}"></td></tr> </form> 搜索: 如

我想通过在文本字段中插入一个单词来搜索整个新闻提要。我不知道怎么做。这是我的代码,所以你知道我的意思

<form action="search.php" method="get">
    <tr><th>search: </th><td><input type="text" name="search" value="{$word}"></td></tr>
</form>

搜索:
如何检查我在搜索栏中插入的单词是否存在于新闻提要中

我的新闻提要是这样使用的:

    $xml=simplexml_load_file("newsfeed.xml");
    foreach($xml->channel->item as $item) {
        echo '<h1 class="title">' . $item->title . '</h1>';
        echo '<p class="desc">'.$item->description."</p>";
}
$xml=simplexml\u加载文件(“newsfeed.xml”);
foreach($xml->channel->item as$item){
回显“.$item->title.”;
echo'

。$item->description.“

”; }
我会像你现在这样做: 并为每一条新闻做相关点。比如,如果它在标题中,那么它就更相关,如果它在开头,等等。 然后您可以将其写入数组并对其进行排序。
我希望您的newsfeed.xml中有类似于此的id。

我认为您可以使用此函数

查找中第一次出现的指针的数字位置 草垛绳

例如:

$a = 'Long text to look into it.'

if (strpos($a, 'it') !== false)
    echo 'true';
在您的情况下,您可以使用strpos在项目标题或项目描述中查找单词:

$a = $_GET['search'];

foreach($xml->channel->item as $item) {
    print_result = 0;  // flag to know if the search is in the feed.

    if (strpos($item->description, $a) !== false){
        print_result = 1;
    } // end if desc

    if (strpos($item->title, $a) !== false){
        print_result = 1;
    } // end if title


    if(print_result == 1){
        echo '<h1 class="title">' . $item->title . '</h1>';
        echo '<p class="desc">'.$item->description."</p>";
    } //end if print results.
} // end foreach
$a=$\u GET['search'];
foreach($xml->channel->item as$item){
print_result=0;//标记以了解搜索是否在提要中。
if(strpos($item->description,$a)!==false){
打印结果=1;
}//如果描述结束
if(strpos($item->title,$a)!==false){
打印结果=1;
}//如果标题为,则结束
如果(打印结果==1){
回显“.$item->title.”;
echo'

。$item->description.“

”; }//如果打印结果,则结束。 }//结束foreach
这取决于你想要什么样的结果。不幸的是,在PHP(或其他语言)中,没有一种方法可以达到Google和Bing在一两行中所获得的相关性。我建议阅读不同的搜索技术,如通配符、全文搜索、索引、词根分析等,并选择一种适合您需要的方法。如果使用$u POST['search',可以查看一个非常简单但大胆的方法在$item->title或$item->description中。如果您搜索
Bus
,这不会突出显示业务吗?@Mike您是对的!它将显示包含“bus”的字符的所有匹配结果。如果您只需要“总线”,则需要进行更多检查。请注意,
strpos
区分大小写。如果要清理用户输入。。您可以使用
stripos
,它不区分大小写。。或者在用户输入和馈送项目中使用
strtolower
,以匹配您的单词……正如@MikeB所指出的,您可以使用按单词搜索;您需要构建正则表达式。