weixin_33738578 2017-11-11 10:07 采纳率: 0%
浏览 14

Ajax搜索POST到php

I need some help with refining my current search.

I have folder with images that are named as:

20171116-category_title.jpg       (where first number is date yyyymmdd)

My current search looks like this:

<?php
// string to search in a filename.

if(isset($_POST['question'])){
    $searchString = $_POST['question'];
}
// image files in my/dir
$imagesDir = '';
$files = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

// array populated with files found 
// containing the search string.
$filesFound = array();

// iterate through the files and determine 
// if the filename contains the search string.
foreach($files as $file) {
    $name = pathinfo($file, PATHINFO_FILENAME);

    // determines if the search string is in the filename.
    if(strpos(strtolower($name), strtolower($searchString))) {
         $filesFound[] = $file;
    } 
}

// output the results.
echo json_encode($filesFound, JSON_UNESCAPED_UNICODE);
?>

And this works just fine but...

  1. I would like to limit search only to part of .jpg name that contains "title" behind underscore " _ " and after that (if possible) to expand search to:

  2. To make double search if AJAX POST sends following format: abc+xyz where delimiter "+" practicaly means 2 queries.

    First part is (abc) which targets "category" that stands between minus and underscore and second part of query (xyz) (which is basically my first question) only among previously found (category) answers.

    Your tips are more than welcome! Thank you!

  • 写回答

1条回答 默认 最新

  • weixin_33736832 2017-11-11 10:29
    关注

    For the first part of your question, the exact pattern you use depends on the format of your category strings. If you will never have underscores _ in the category, here's one solution:

    foreach($files as $file) {
        // $name = "20171116-category_title"
        $name = pathinfo($file, PATHINFO_FILENAME);
    
        // $title = "title", assuming your categories will never have "_".
        // The regular expression matches 8 digits, followed by a hyphen, 
        // followed by anything except an underscore, followed by an 
        // underscore, followed by anything
        $title = preg_filter('/\d{8}-[^_]+_(.+)/', '$1', $name);
    
        // Now search based on your $title, not $name
        // *NOTE* this test is not safe, see update below.
        if(strpos(strtolower($title), strtolower($searchString))) {
    

    If your categories can or will have underscores, you'll need to adjust the regular expression based on some format you can be sure of.

    For your 2nd question, you need to first separate your query into addressable parts. Note though that + is typically how spaces are encoded in URLs, so using it as a delimiter means you will never be able to use search terms with spaces. Maybe that's not a problem for you, but if it is you should try another delimter, or maybe simpler would be to use separate search fields, eg 2 inputs on your search form.

    Anyway, using +:

    if(isset($_POST['question'])){
        // $query will be an array with 0 => category term, and 1 => title term
        $query = explode('+', $_POST['question']);
    }
    

    Now in your loop you need to identify not just the $title part of the filename, but also the $category:

    $category = preg_filter('/\d{8}-([^_]+)_.+/', '$1', $name);
    $title = preg_filter('/\d{8}-[^_]+_(.+)/', '$1', $name);
    

    Once you have those, you can use them in your final test for a match:

    if( strpos(strtolower($category), strtolower($query[0])) && strpos(strtolower($title), strtolower($query[1])) ) {
    

    UPDATE

    I just noticed your match test has a problem. strpos can return 0 if a match is found starting at position 0. 0 is a falsey result which which means your test will fail, even though there's a match. You need to explicitly test on FALSE, as described in the docs:

    if( strpos(strtolower($category), strtolower($query[0])) !== FALSE 
        && strpos(strtolower($title), strtolower($query[1])) !== FALSE ) {
    
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制