我有这段代码,我必须开发表单批处理文件。那是旧系统。
以下代码扫描./wp-content/uploads/webvideos/
并列出所有具有MP4扩展名的文件。它工作得很好,但我需要过滤结果。
$post_title = html_entity_decode(get_the_title());
是WORDPRESS Post的标题。例如,在这种情况下,它是: V000-TEST READ ALL
过滤器应该能够过滤 V000-TEST READ ALL 的以下变体
如您所见,唯一保持不变的是标题。 TEST READ ALL
我不知道从哪里开始,更不用说写什么了。尽管学习,PHP并不是我走的路。
$path = "./wp-content/uploads/webvideos/";
$webdir = site_url()."/wp-content/uploads/webvideos/";
$webdirclean = site_url();
$post_title = html_entity_decode(get_the_title());
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS));
$files = array();
$notAllowedExtension = array('jpg', 'vtr', 'html', 'htm' );
foreach($iterator as $file){
if(!in_array($file->getExtension(), $notAllowedExtension) && !$file->isDir())
$files[filemtime($file->getPathname())] = $file->getPathname();
}
ksort($files);
// Filter Array based on name, including varation name
$files = array_filter($files, function($post_title)
{
return preg_match('#^V\d{3}\s*-\s*$post_title(\s*-\s*[^\s].+)?$#', $post_title);
});
echo '<h2>After filter</h2>';
echo implode('<br>', $files);
//echo "<pre>" . print_r($files, true) . "</pre>";
foreach($files as $file => $date){
$title=$files[$file];
$cleantitle = basename($title, ".mp4");
if( $i==$autoplay ) $file_main = $title;
$output .= "" . PHP_EOL .
"<tr>
<td><a href=\"javascript:loadVideo('$webdirclean$title')\"> $cleantitle (Click Me)</a>
</td>
<td>NA</td>
</tr>"
. PHP_EOL;
$i++;
}
作为一种参考,我不确定它是否有用,这是我之前制作的批量代码,它可以满足我的需要。
rem Separate file name in parts delimited by "-. "
rem and use each part to test the element in folder array
set "name=%file_name%"
:nextPart
for /F "tokens=1* delims=-. " %%a in ("%name%") do (
if not defined folder[%%a] (
set "name=%%b"
goto nextPart
) else (
set "folder_path=!folder[%%a]!"
goto pathFound
)
)
rem Search the elements of folder array in the file_name
for /F "tokens=2,3 delims=[]=" %%a in ('set folder[') do (
if "!file_name:%%a=!" neq "%file_name%" (
set "folder_path=%%b"
goto pathFound
)
)
echo ERROR: Folder path not found
答案 0 :(得分:2)
您可以使用正则表达式过滤标题:
<?php
$post_titles =
[
'V001-TEST READ ALL',
'V002-TEST READ ALL',
'V003 -TEST READ ALL',
'V004- TEST READ ALL',
'V005- TEST READ ALL',
'V006-TEST READ ALL - Some Extra Information',
'V007-TEST READ ALL - There will always be a - for extra info',
'V008-TEST READ ALL - This work is complex Bla bla ABAK (19283)',
'V009-TEST READ ALL - Final Version',
// Some faulty titles:
'V009-TEST READ NONE',
'V 001-TEST READ ALL',
'V001-TEST READ ALL - ',
'V001- - This work is complex Bla bla ABAK (19283)'
];
echo '<h2>Before filter</h2>';
echo implode('<br>', $post_titles);
$var = 'TEST READ ALL'; // The fixed portion of the string
$post_titles = array_filter($post_titles, function($post_title) use($var)
{
return preg_match("#^V\d{3}\s*-\s*$var(\s*-\s*[^\s].+)?$#", $post_title);
});
echo '<h2>After filter</h2>';
echo implode('<br>', $post_titles);
?>
<强>输出:强>
<h2>Before filter</h2>V001-TEST READ ALL<br>V002-TEST READ ALL<br>V003 -TEST READ ALL<br>V004- TEST READ ALL<br>V005- TEST READ ALL<br>V006-TEST READ ALL - Some Extra Information<br>V007-TEST READ ALL - There will always be a - for extra info<br>V008-TEST READ ALL - This work is complex Bla bla ABAK (19283)<br>V009-TEST READ ALL - Final Version<br>V009-TEST READ NONE<br>V 001-TEST READ ALL<br>V001-TEST READ ALL - <br>V001- - This work is complex Bla bla ABAK (19283)<h2>After filter</h2>V001-TEST READ ALL<br>V002-TEST READ ALL<br>V003 -TEST READ ALL<br>V004- TEST READ ALL<br>V005- TEST READ ALL<br>V006-TEST READ ALL - Some Extra Information<br>V007-TEST READ ALL - There will always be a - for extra info<br>V008-TEST READ ALL - This work is complex Bla bla ABAK (19283)<br>V009-TEST READ ALL - Final Version