Home > database >  Detect the right name in string
Detect the right name in string

Time:12-29

So I was wondering how to detect the right files, by their names, when I scan through them.

Right now when I open a pop-up window, I GET a id (?id=2451961), and this id is used to detect image files in a folder. But how should i detect them?

Is there a way to say, the start of the files name to the first non-number should be the id, and if it's equal to the id then thats one of the files?

The folder with some files could be this:

enter image description here

Right now I loop through the files like this, but it doesn't get the file '2451961 - Copy.png':

foreach ($list as $file) {
    $file_name = strtolower(substr(strtok($file, '.'),0));
    $type = strtolower(substr(strrchr($file,"."),1));
    if ($type != "log" ) {
        if ((strtok($file, '_') == $id) || $file_name == $id) {
            $scr = '../test/ftp_image.php?img='.$file;
            ?>
            <img src="<?php echo $scr; ?>" height="250px"/> <?php

            echo $file;
        }
    }
}

Note: there is a statement exclude = .log files in the code, which is because there is some files containing text which shouldn't be takes into consideration.

The files i want to get in this example is these:

enter image description here

NOTE: Not all images will be .png, there could be a .jpg or something like that.

file names:

2451961 - Copy.png
2451961.jpg
2451961 - Copy 2.png
2451961 - Copy_2.jpeg

CodePudding user response:

Regex looks like a better/right tool for this job. Your regex could look like below:

'/^'.preg_quote($id).'\D/'

preg_quote is just for escaping of any regex metacharacters. So, your file name should start with the ID followed by a non digit, which is \D. We won't have to care about file extensions if we do it this way.

Snippet:

<?php

$files = [
    '2451961 - Copy.png',
    '2451961.png',
    '2451961_2 - Copy.png',
    '2451961_2.png',
    '4405.png'
];

$id = '2451961';

foreach($files as $file){
    echo $file, " ", var_dump(preg_match('/^'.preg_quote($id).'\D/', $file) === 1),PHP_EOL;
}

Online Demo

CodePudding user response:

Given that you are unable to use glob then perhaps combining preg_grep with ftp_nlist directly might be a suitable method once you have build a suitable regex

$id='2451961';
$pttn=sprintf('@(%1$s. ?\.png|%1$s. ?\.jpg|%1$s. ?\.jpeg|%1$s. ?\.gif)@',$id );
$col=preg_grep( $pttn, ftp_nlist( $conn, $dir ) );
print_r($col);

CodePudding user response:

Try use strpos to search needle substring. Its return you false if not found what you want and code will be like that

if (strpos($file, $id) !== false) {
        $scr = '../test/ftp_image.php?img='.$file;
        ?>
        <img src="<?php echo $scr; ?>" height="250px"/> <?php

        echo $file;
    }

UPDATE Thx Movs to correct this code

CodePudding user response:

Try to use PCRE like:

$pattern = '/\b' . preg_quote($id, '/') . '(?:_\d )?(?: - Copy)?\.(?!log)[a-z] $/';
foreach ($list as $file) {
  if (preg_match($pattern, $file)) {
    $scr = '../test/ftp_image.php?img='.$file;
    ?><img src="<?php echo $scr; ?>" height="250px"/> <?php

    echo $file;
  }
}

An example of how it works here.

In pattern (for example $id be equal '123'):

  • \b — work break. '12345' match, '0123' doesn't match
  • preg_quote($id, '/') — escaping $id value from PCRE syntactic chars
  • (?:_\d )? — adds potential support of 123_1, 123_34, etc file name suffixes
  • (?: - Copy)? — adds potential supporting of 123 - Copy file name
  • (?:_\d )?(?: - Copy)? — (combination) adds potential supporting of 123_2 - Copy file names
  • \.(?!log)[a-z] $ — disables the .log file extensions while allowing all others
  •  Tags:  
  • php
  • Related