file_get_contents无法获取正确文件

时间:2016-10-11 02:18:07

标签: php arrays file-get-contents

我正在尝试构建一个脚本,该脚本将根据州名显示新的IP。 IP应该来自与州名同名的文件。然后它需要一个随机的IP并分配它(这是它应该如何工作)。

问题在于它分配了完全随机的IP并从随机文件中加载IP,而不是从具有州名的正确文件中获取IP。

这应该如何运作:加州> california.txt>随机IP

现在如何运作:加州> newyork / georgia / texas / etc.txt>随机IP

$state_content=["Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","NewHampshire","NewJersey","NewMexico","NewYork","NorthCarolina","NorthDakota","Ohio","Oklahoma","Oregon","Pennsylvania","RhodeIsland","SouthCarolina","SouthDakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","WestVirginia","Wisconsin","Wyoming"];

$country_content=["UnitedStates", "UnitedKingdom", "Canada", "Australia", "Germany", "France", "Spain"];

$a = 0;

function getStateContent($state_name) {

    // checks do we already have content for this state
    if(!isset($state_content[$state_name])) {

        // generate file name
        $file_name = "state/";
        $file_name .= str_replace(" ", "", ucwords($state_name));
        $file_name .= ".txt";

        $state_text = file_get_contents($file_name);
        $state_content[$state_name] = explode("\n", $state_text);
    }

    return $state_content[$state_name];
}

function getStateIpByName($state_name) {
    $content = getStateContent($state_name);
    return $content[array_rand($content)];
}

function getCountryContent($country_name) {

    // checks do we already have content for this state
    if(!isset($country_content[$country_name])) {

        // generate file name
        $file_name = "country/";
        $file_name .= str_replace(" ", "", ucwords($country_name));
        $file_name .= ".txt";

        $country_text = file_get_contents($file_name);
        $country_content[$country_name] = explode("\n", $country_text);
    }

    return $country_content[$country_name];
}

function getCountryIpByName($country_name) {
    $content = getCountryContent($country_name);
    return $content[array_rand($content)];
}

用法:

if ($result->num_rows > 0) {
    // output data of each row
    echo "<div class='block'><div class='rows'>";

    while($row = $result->fetch_assoc()) {

        if (!empty($row["state"]) && $row["country"] == "united states") {
            $stateip = getStateIpByName($row["state"]);
            $a++;
            echo $stateip;
        }

        else if (empty($row["state"]) && $row["country"] == "united states") {
            $countryip = getCountryIpByName($row["country"]);
            $a++;
            echo $countryip;
          }

        else if ($row["country"] != "united states") {

        }

    }

1 个答案:

答案 0 :(得分:1)

正如我在评论中所说,

每次从$state_content[$state_name] = explode("\n", $state_text);循环调用相应的函数时,这些语句$country_content[$country_name] = explode("\n", $country_text);while()都会操纵/更改原始数组。

解决方案是,(在讨论聊天后)

按以下方式更改getStateContent()getCountryContent()功能

function getStateContent($state_name) {
    // checks do we already have content for this state
    global $state_content;
    $state_name = str_replace(" ", "", ucwords($state_name));
    if(in_array($state_name, $state_content)) {
        // generate file name
        $file_name = "state/";
        $file_name .= $state_name;
        $file_name .= ".txt";

        $state_text = file_get_contents($file_name);
        $ipaddresses = explode("\n", $state_text);
        return $ipaddresses;
    }
}

function getCountryContent($country_name) {
    // checks do we already have content for this state
    global $country_content;
    $country_name = str_replace(" ", "", ucwords($country_name));
    if(in_array($country_name, $country_content)) {
        // generate file name
        $file_name = "country/";
        $file_name .= $country_name;
        $file_name .= ".txt";

        $country_text = file_get_contents($file_name);
        $ipaddresses = explode("\n", $country_text);
        return $ipaddresses;

    }
}