Ajax搜索文章到php
我需要一些帮助来改进我当前的搜索 我有一个文件夹,其中的图像名为:Ajax搜索文章到php,php,ajax,Php,Ajax,我需要一些帮助来改进我当前的搜索 我有一个文件夹,其中的图像名为: 20171116-category_title.jpg (where first number is date yyyymmdd) 我当前的搜索结果如下所示: <?php // string to search in a filename. if(isset($_POST['question'])){ $searchString = $_POST['question']; } // image fi
20171116-category_title.jpg (where first number is date yyyymmdd)
我当前的搜索结果如下所示:
<?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);
?>
这很好,但是
对于问题的第一部分,您使用的确切模式取决于
类别
字符串的格式。如果在类别中永远不会有下划线。
,这里有一个解决方案:
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))) {
如果您的类别可以或将有下划线,则需要根据您可以确定的某种格式调整正则表达式
对于第二个问题,您需要首先将查询分为可寻址部分。请注意,+
通常是URL中空格的编码方式,因此将其用作分隔符意味着您永远无法将搜索词与空格一起使用。也许这对你来说不是问题,但如果是的话,你应该尝试另一个delimter,或者更简单的方法是使用单独的搜索字段,例如在搜索表单上输入两个输入
无论如何,使用+
:
if(isset($_POST['question'])){
// $query will be an array with 0 => category term, and 1 => title term
$query = explode('+', $_POST['question']);
}
现在,在循环中,您不仅需要识别文件名的$title
部分,还需要识别$category
:
$category = preg_filter('/\d{8}-([^_]+)_.+/', '$1', $name);
$title = preg_filter('/\d{8}-[^_]+_(.+)/', '$1', $name);
一旦你有了这些,你可以在比赛的最后测试中使用它们:
if( strpos(strtolower($category), strtolower($query[0])) && strpos(strtolower($title), strtolower($query[1])) ) {
更新
我刚注意到你的比赛测试有问题<如果从位置0
开始找到匹配项,则code>strpos
可以返回0
0
是一个错误的结果,这意味着您的测试将失败,即使存在匹配项。您需要显式测试FALSE
,:
对于问题的第一部分,您使用的确切模式取决于
类别
字符串的格式。如果在类别中永远不会有下划线。
,这里有一个解决方案:
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))) {
如果您的类别可以或将有下划线,则需要根据您可以确定的某种格式调整正则表达式
对于第二个问题,您需要首先将查询分为可寻址部分。请注意,+
通常是URL中空格的编码方式,因此将其用作分隔符意味着您永远无法将搜索词与空格一起使用。也许这对你来说不是问题,但如果是的话,你应该尝试另一个delimter,或者更简单的方法是使用单独的搜索字段,例如在搜索表单上输入两个输入
无论如何,使用+
:
if(isset($_POST['question'])){
// $query will be an array with 0 => category term, and 1 => title term
$query = explode('+', $_POST['question']);
}
现在,在循环中,您不仅需要识别文件名的$title
部分,还需要识别$category
:
$category = preg_filter('/\d{8}-([^_]+)_.+/', '$1', $name);
$title = preg_filter('/\d{8}-[^_]+_(.+)/', '$1', $name);
一旦你有了这些,你可以在比赛的最后测试中使用它们:
if( strpos(strtolower($category), strtolower($query[0])) && strpos(strtolower($title), strtolower($query[1])) ) {
更新
我刚注意到你的比赛测试有问题<如果从位置0
开始找到匹配项,则code>strpos
可以返回0
0
是一个错误的结果,这意味着您的测试将失败,即使存在匹配项。您需要显式测试FALSE
,:
你解决了这个问题吗?我的回答有帮助吗?我很抱歉没有感谢你。我离开了我的工作,所以我还没有实施你的解决方案,但我看到它会没事的。如果由于任何原因我再次陷入困境,我会尽量联系你,如果可以的话?再次感谢大家!!!!你解决了这个问题吗?我的回答有帮助吗?我很抱歉没有感谢你。我离开了我的工作,所以我还没有实施你的解决方案,但我看到它会没事的。如果由于任何原因我再次陷入困境,我会尽量联系你,如果可以的话?再次感谢大家!!!!