<?php
$string = file_get_contents("csv.csv");
$array = explode(",", $string);
$q = strtolower($_GET[q]);
foreach ($array as $value) {
$result = explode(":", $value);
if (strpos($q, $result[0]) !== false) {
$output = $result[1];
}
}
echo $output;
?>
这是csv.csv
我要变成字符串的文件的内容。
hello: how are you doing,
hi: what are you,
df:df
如果$_GET[q]
(和$q
)是hello
,则$output
是how are you doing
。但是,如果是hi
,则不会得到输出,what are you
或者如果没有df
,则不会得到df
。
为什么会这样呢?预先感谢您的帮助。
您正在用逗号爆炸,但事实是您用逗号加上换行符分隔了每个值。
爆炸后,您的数组为[“ hello”,“ \ nhi ...”,“ \ ndf:...”],这就是为什么strpos比较不匹配的原因。
尝试
$array = explode(",\n", $string);
编辑:正如@Michael Berkowski所说,您也可以修整参数
if (strpos($q, trim($result[0])) !== false)
参数的顺序取决于您要提供哪种部分匹配。根据您当前的参数顺序,参数“ hi”将匹配“ hi”,“ h”和“ i”,但不匹配“ high”。
如果按照迈克尔的建议翻转它们,则参数“ hi”将匹配“ hi”和“ high”,但不匹配“ h”或“ i”。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句