这是代码
$description = explode("<li>", $rows['description']);
var_dump($description);
$find = 'annual';
$key = array_search($find, $description);
var_dump($key);
//echo $description[$key];
这是输出:
array(9) {
[0]=> string(4) " "
[1]=> string(185) "Fair. No annual fee. No overlimit fee. No foreign transaction fee. Pay up to midnight ET online or by phone on your due date without a fee. Plus, paying late won't raise your APR.*"
[2]=> string(183) "Generous. 5% cash back at Home Improvement Stores & More on up to $1,500 in purchases from April through June 2014 when you sign up. And 1% cash back on all other purchases.*"
[3]=> string(64) "Human. 100% U.S.-based customer service available any time."
[4]=> string(188) "Looks out for you-since each Discover purchase is monitored. If it's unusual, you're alerted by e-mail, phone or text-and never responsible for unauthorized Discover card purchases.*"
[5]=> string(106) "Plus, free FICO® Credit Score on your monthly statement to help you stay on top of your credit.*"
[6]=> string(171) "0% Intro APR* on balance transfers for 18 months. Then the variable purchase APR applies, currently 10.99% - 22.99%. A fee of 3% applies for each balance transferred."
[7]=> string(112) "0% Intro APR* on purchases for 6 months. Then the variable purchase APR applies, currently 10.99% - 22.99%."
[8]=> string(119) "*Click "Apply" to see rates, rewards, and free FICO® Credit Score terms and other information.
" }
bool(false)
变量$find
正在输出中搜索"annual"
,您可以看到数组键1中有year,但它返回false。
所以我不知道我错过了什么或做错了什么。我试着用数组1的整个值来测试它,以确保在数组内部搜索没有问题,仍然是错误的。也更改了$find = "Generous"
相同的结果。。。错误
您误解了array_search()
的功能。以下行:
公平。无年费。无超限额费用。不收取国外交易费用。在线或在预产期通过电话支付至美国东部时间午夜,不收取任何费用。此外,延迟付款不会提高您的APR*
包含字符串annual
,但它不是完整的字符串annual
。换句话说,array_string()
不在字符串内部搜索,而是尝试完全匹配。
为了找到你正在寻找的结果,我会尝试以下内容:
$matches = array_filter($description, function($el) {
// evaluate the current element
// return true if a string index is
// found for the target string
return strpos($el, 'annual') !== false;
});
var_dump($matches);
这对$description
数组进行迭代,并返回一个数组$filtered
,该数组包含包含字符串annual
的任何元素。或者,您可以使用foreach
循环,并将每个匹配的示例添加到$matches
数组中。
此处的示例:https://eval.in/146145
希望这能有所帮助。
- http://ie1.php.net/strpos
- http://www.php.net/manual/en/function.array-filter.php