出于某种奇怪的原因
if(in_array("January", $season)){
当我指定" 1月"在in_array中它匹配数组值。但是,当我使用包含单词January的$search_string
时,它并不匹配。出现这种情况的原因是什么我在单词之前和之后删除了空格,但它仍然不匹配。
$season = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
print_r($lines);
Array ( [0] => January - March, November
[1] => 2 travelers: Adult: $9,499 Child: $7,125
[2] => 4 travelers: Adult: $8,315 Child: $6,235
[3] => 6 travelers: Adult: $7,975 Child: $5,980
[4] => June - October, December
[5] => 2 travelers: Adult: $10,799 Child: $8,099
[6] => 4 travelers: Adult: $9,835 Child: $7,375
[7] => 6 travelers: Adult: $9,535 Child: $7,150
)
var_dump($lines);
array(8) { [0]=> string(47) "January - March, November
" [1]=> string(48) "2 travelers: Adult: $9,499 Child: $7,125
" [2]=> string(49) "4 travelers: Adult: $8,315 Child: $6,235
" [3]=> string(47) "6 travelers: Adult: $7,975 Child: $5,980
" [4]=> string(46) "June - October, December
" [5]=> string(49) "2 travelers: Adult: $10,799 Child: $8,099
" [6]=> string(49) "4 travelers: Adult: $9,835 Child: $7,375
" [7]=> string(47) "6 travelers: Adult: $9,535 Child: $7,150
"
}
foreach($lines as $key => $value){
$search_string = explode("-", $value);
$search_string = $search_string[0];
echo "--"; echo $search_string = trim($search_string); echo "--"; // --January--
if(in_array("January", $season)){
echo "Match found";
} else {
echo "Match not found";
}
}
@spirit,这里是创建数组行的地方
$lines = array();
foreach(preg_split("/((\r?\n)|(\r\n?))/", $seasonal_price) as $line){
// Only add to array for lines with content
if(trim($line)!="") $lines[] = $line;
}
这是我的最终代码和最终输出:
//Seasonal price
$seasonal_price = $row['post_content'];
//Split individual lines into an array
$lines = preg_split("/[\r\n]+/", strip_tags($seasonal_price), -1, PREG_SPLIT_NO_EMPTY);
//Define seasons
$season = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
echo "<br>";
foreach ($lines as $key => $value) {
$search_string = explode("-", $value);
$search_string = trim($search_string[0]);
if (in_array($search_string, $season)) {
echo '<i class="fa fa-money"></i> <span class="head" style="font-weight:bold;">';echo$value;echo'</span>';
} else {
echo '<span> ';echo$value;echo'</span>';
}
echo "<br>";
}
?>
代码部分反映了标签 1月和6月
下图中的结果问题是由于浏览器窗口中的html容器,我没有看到隐藏的<strong>
标记。我没有看到标签,它被解析为html标签。
答案 0 :(得分:1)
尝试以下方法:
$lines = preg_split("/[\r\n]+/", strip_tags($seasonal_price), -1, PREG_SPLIT_NO_EMPTY);
foreach ($lines as $key => $value) {
$search_string = explode("-", $value);
$search_string = trim($search_string[0]);
//echo "--"; echo $search_string = trim($search_string); echo "--"; // --January--
if (in_array($search_string, $season)) {
echo "Match found";
} else {
echo "Match not found";
}
echo "\r\n";
}
输出:
Match found
Match not found
Match not found
Match not found
Match found
Match not found
Match not found
Match not found
就像你的输入$lines
一样。
更新:正如我们在聊天中发现的那样,填充$seasonal_price
的数据来自数据库并且包含:
<strong>January - March, November</strong><br/>
2 travelers: Adult: $9,499 Child: $7,125<br/>
4 travelers: Adult: $8,315 Child: $6,235<br/>
6 travelers: Adult: $7,975 Child: $5,980<br/>
<strong>June - October, December</strong><br/>
2 travelers: Adult: $10,799 Child: $8,099<br/>
4 travelers: Adult: $9,835 Child: $7,375<br/>
6 travelers: Adult: $9,535 Child: $7,150<br/>
所以我在我的回答中添加了strip_tags()
来电