变量不起作用? / in_array

时间:2017-12-30 18:46:47

标签: php html

我有两个不想与in_array互动的变量,我突然想到它们。

(第一部分)主要代码添加:

//Get Countrys
$Countrys = str_get_html($Disthtml);
$Countrys2 = $Countrys->find('span[class=regions regionsInt]');

//Get Departure Country
$DepartureC = $Countrys2[0];
$DepartureC2 = explode(',', $DepartureC);
$DepartureC3 = end($DepartureC2);
$DepartureC4 = str_replace(' ', '', $DepartureC3);
$DepartureCountry = $DepartureC4;

//Get Arrival Country
$ArrivalC = $Countrys2[1];
$ArrivalC2 = explode(',', $ArrivalC);
$ArrivalC3 = end($ArrivalC2);
$ArrivalC4 = str_replace(' ', '', $ArrivalC3);
$ArrivalCountry = $ArrivalC4;

(第二部分)主要代码:

//Check if Departure Country or Arrival Country is inside the EU
$CountrysEU = array('Austria', 'Belgium', 'Bulgaria', 'Croatia', 'Cyprus', 'CzechRepublic', 'Denmark', 'Estonia', 'Finland', 'France', 'Germany', 'Greece', 'Hungary', 'Ireland', 'Italy', 'Latvia', 'Lithuania', 'Luxembourg', 'Malta', 'Netherlands', 'Poland', 'Portugal', 'Romania', 'Slovakia', 'Slovenia', 'Spain', 'Sweden', 'UnitedKingdom');

if(in_array($DepartureCountry, $CountrysEU) || in_array($ArrivalCountry, $CountrysEU)){
    echo 'EU';
}
else {
    echo 'NotEU';
}

变量$ArrivalCountry$DepartureCountry从网页上被抓取(curl php)并进行操作,因此如果他们在欧盟,他们应该通过名称来适应欧盟国家/地区。

Var_dump两个var:

$ArrivalCountry: string(13) "Russia" 
$DepartureCountry: string(20) "UnitedKingdom"
  

为什么in_array不能处理我的变量,它只输出NotEU

注意:

如果我像这样手动定义变量:

$ArrivalCountry = 'Russia'; 
$DepartureCountry = 'UnitedKingdom';

代码完美无缺。

1 个答案:

答案 0 :(得分:2)

运行代码后,我知道您的两个国家/地区都有</span>,并且由于编码问题,他们没有显示并让您感到困惑。

您必须使用以下代码才能使其正常工作: -

<?php
header('Content-type: text/plain; charset=utf-8'); // add this encoding
include('simple_html_dom.php'); // i think you already have this

 // Your CURL CODE
 $Disthtml = curl_exec($ch2); // get Data from curl


$Countrys = str_get_html($Disthtml);

$Countrys = $Countrys->find('span[class=regions regionsInt]');

//Get Departure Country
$DepartureCountry = str_replace('</span>','',trim(end(explode(',', $Countrys[0])))); // remove unnecessary variable creation

//Get Arrival Country
$ArrivalCountry = str_replace('</span>','',trim(end(explode(',', $Countrys[1])))); // remove unnecessary variable creation


$CountrysEU = array('Austria', 'Belgium', 'Bulgaria', 'Croatia', 'Cyprus', 'CzechRepublic', 'Denmark', 'Estonia', 'Finland', 'France', 'Germany', 'Greece', 'Hungary', 'Ireland', 'Italy', 'Latvia', 'Lithuania', 'Luxembourg', 'Malta', 'Netherlands', 'Poland', 'Portugal', 'Romania', 'Slovakia', 'Slovenia', 'Spain', 'Sweden', 'United Kingdom');

if(in_array($ArrivalCountry, $CountrysEU) || in_array($DepartureCountry, $CountrysEU)){
    echo 'EU';
}
else {
    echo 'NotEU';
}