doulai2573 2010-04-01 18:18
浏览 50
已采纳

PHP:如果没有结果 - 拆分Searchrequest并尝试查找搜索的部分内容

i want to split the searchrequest into parts, if there's nothing to find. example: "nelly furtado ft. jimmy jones" -> no results -> try to find with nelly, furtado, jimmy or jones.. i have an api url.. thats the difficult part.. i show you some of the actually snippets:

$query = urlencode (strip_tags ($_GET[search]));

and

 $found = '0';
    if ($source == 'all')
    {
      if (!($res = @get_url ('http://api.example.com/?key=' . $API . '&phrase=' . $query . ' . '&sort=' . $sort)))
      {
        exit ('<error>Cannot get requested information.</error>');
        ;
      }

how can i put a else request in this snippet, like if nothing found take the first word, or the second word, is this possible? or maybe you can tell me were i can read stuff about this function?

thank you!!

  • 写回答

2条回答 默认 最新

  • du8980919 2010-04-01 18:31
    关注

    Well, you've used urlencode() on $query, so after that all the words will be separated by a + character. You can then split the words out like this:

    $words = explode('+', $query);
    

    After that, just do a for loop over each of the words and send queries again (I don't know what your $source = 'all' line is for, so I'll just leave it there).

    for ($i = 0; $i < count($words); $i++)
    {
        if ($source == 'all')
        {
            if (!($res = @get_url ('http://api.example.com/?key=' . $API . '&phrase=' . $words[$i] . ' . '&sort=' . $sort)))
            {
                exit ('<error>Cannot get requested information.</error>');
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 w10部分软件不能联网
  • ¥15 关于安装hbase的问题(操作系统-windows)
  • ¥15 cadence617版本,如何做一个参数可调的反相器
  • ¥15 novnc连接pve虚拟机报错安全协议不支持262
  • ¥15 设备精度0.03给多少公差能达到CPK1.33
  • ¥15 qt+ffmpeg报错non-existing PPS 0 referenced
  • ¥15 FOC simulink
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配