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


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:

// string to search in a filename.

    $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 +:

        // $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])) ) {


    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 实验七:Pandas要有实验截图和代码
    • ¥15 TypeError: Make sure that the iterable only contains strings.
    • ¥35 电脑放图书馆,这是被黑了吗
    • ¥60 关于SQL里,无法利用游标语句修改表中的数据,如何解决?(相关搜索:sql语句)
    • ¥15 等高线中数据取消科学计数法
    • ¥15 Qt播放10路ffmpeg 视频流
    • ¥15 如何利用闲置机械硬盘变现
    • ¥15 信号处理中的凸优化问题
    • ¥15 arm虚拟机无法和物理机互通
    • ¥15 Android导航条遮盖异常