2

I have two variables that don´t want to interact with in_array, I´m breaking my head on them.

(First Part) Addition to Maincode:

//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;

(Second Part) Maincode:

//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';
}

The variables $ArrivalCountry and $DepartureCountry are scraped (curl php) from a webpage and manipulated so they should fit the EU Countries by names if they are in the EU.

Var_dump of the two var:

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

Why doesn´t in_array work with my variables, it just outputs NotEU.

Note:

If I define the variables manually like this:

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

The code works perfectly.

2
  • 1
    Looks encoding problem, your characters in strings looks to be more than 1 byte. Commented Dec 30, 2017 at 19:19
  • 1
    @Muhammad yes it's encoding problem Commented Dec 30, 2017 at 19:43

1 Answer 1

2

After running your code i got to know that your both countries have </span> in it and due to encoding problem they are not showing and confusing you.

You have to use below code to make it work:-

<?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';
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.