【问题标题】:search array - php搜索数组 - php
【发布时间】:2011-07-07 13:48:05
【问题描述】:

我试图找出如何搜索字符串以查找给定变量,如果它长于两个字符并且存在于数组中,则返回该字符串并以某种方式突出显示搜索词。我已经尝试了两天,但它不正确,非常欢迎任何帮助!

我的 php:

$search = $_POST    ['search']; 
$text = 'some long text containg several words.';

$search = trim($search);
$search = strtolower($search);

$text = explode($text);

if ((in_array($search, $text)) && ($search >= 2)){
    echo $text;
}else{
    echo "no result or too short search word";
}

还有我的html:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
    <p>word for which to search:</P>
    <form action="searchfunction.php" method="post">
            <input type="text" name="search" /><br />
            <input type="submit" />
        </form>

     </body>
     </html>

【问题讨论】:

  • 所以现在,感谢 Michael,php 看起来像 Michaels 示例中的那样,但搜索结果只显示任何单词太短或不在数组中,即使我输入了一个我知道的单词数组...关于为什么的任何想法???
  • 用var_dump($_POST);检查脚本接收到的实际$_POST

标签: php arrays search


【解决方案1】:

似乎根本不需要将explode() 放入数组中。只需使用strpos() 定位搜索字符串并使用strlen() 确保它超过2 个字符。

$search = trim($_POST['search']); 
$text = 'some long text containg several words.';

// Search for $search inside $text as a string
// Both converted to lowercase only for the search    
if (strpos(strtolower($text), strtolower($search)) !== FALSE  && strlen($search) >= 2){
    echo $text;
}else{
    echo "no result or too short search word";
}

现在来处理您的突出显示。最简单的方法是使用str_ireplace() 将其包围在&lt;span&gt; 中。您需要在 CSS 文件中定义“highlight”类以具有一些特殊颜色。

// If your text was found...
$text = str_ireplace($search, "<span class='highlight'>$search</span>", $text);
echo $text;

【讨论】:

  • 你忘了stripos(),strpos() 的不区分大小写版本,无需转换为小写,而且更短:p
  • @Arnar Yngvason 奇怪,但后来我想起了str_ireplace。
  • 我可以明白为什么这应该起作用,但是当我使用它时,我只会得到如果搜索词太短或不在数组中时我应该得到的结果,即使我知道它是...
  • @elin 确保$_POST 输入被正确接收。如果我用$search = 'text'; 测试这段代码,它可以正常工作。
【解决方案2】:

代替

if ((in_array($search, $text)) && ($search >= 2)){

你可以试试

if ((in_array($search, $text)) && (strlen($search) >= 2)){

另外, explode 需要将第一个参数作为“”。所以你必须把它称为

$text = explode(" ", $text)

【讨论】:

    【解决方案3】:

    在下一行使用长度函数

    if ((in_array($search, $text)) && ($search >= 2)){
    

    在上面一行他们需要一些修改

    if ((in_array($search, $text)) && (strlen($search) >= 2)){
    

    【讨论】:

      【解决方案4】:

      explode() 至少需要 2 个参数

      $text = explode(' ', $text);
      

      【讨论】:

        【解决方案5】:

        也许

        $text = explode($text);
        
        if ((in_array($search, $text)) && ($search >= 2)){
        

        ...应该是...

        $text = explode(' ', $text);
        
        if ((in_array($search, $text)) && (strlen($search) >= 2)){
        

        也可以在不使用数组的情况下实现您的目标,例如使用正则表达式匹配:

        $search = $_POST['search'];
        if (strlen($search) < 2) {
            echo "too short search word";
        } else {
            $text = 'some long text containg several words.';
            $pattern = '/\b' . preg_quote($search, '/') . '\b/i';
            if (preg_match($pattern, $text, $matches)) {
                echo $matches[0];
            } else {
                echo "no result";
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-01-19
          • 1970-01-01
          • 2015-05-27
          • 2016-04-01
          • 1970-01-01
          • 2012-11-03
          • 2014-06-27
          相关资源
          最近更新 更多