PHP函数在本地运行但不在循环中运行

时间:2014-06-26 13:52:35

标签: php

我的这个功能和语句在我的页面中正常运行:     $ name =“JÜRGENRÜDIGERHERMANN”;

function nameReplace($name)
{
    $name = str_replace("Ñ", "N", $name);
    $name = str_replace("Ü", "U", $name);
    $name = str_replace("Ç", "C", $name);
    $name = str_replace("Á", "A", $name);
    $name = str_replace("É", "E", $name);
    return $name;   
}


$name = nameReplace($name);
echo "This is the name!" . $name . "<br>";

$orename = "ÑÜÇÁÉ";
$orename = nameReplace($orename);
echo "Forename test: " . $orename . "<br>";

$astname = "ÁÉÑÜÇ";
$astname = nameReplace($astname);
echo "Lastname test: " . $astname . "<br>";

一切工作正常,字符更改为可接受的字符,以便上传到进度数据库。但是稍后在页面中我循环访问数据并从解析的HTML文件中获取字符串:

$familyname = nameReplace($familyname);

上述姓氏nameReplace()失败。如果我只是回应姓氏,它会回响字符串。如果我回显nameReplace($ familyname),则返回空白或false。

有什么想法吗?功能是否具有特定范围?

谢谢, 斯蒂芬。

以下是请求循环的代码:

for($i=1;$i<41;$i++)
{
$textfile = 'interpol_create' . $i. '_' . $date . '.txt';
$myfile = fopen($textfile, "r") or die("Unable to open file!");
if($myfile)
{
    #read through the file line by line
    while (($buffer = fgets($myfile, 4096)) !== false)
    {
        $linenumber++;
        if( (strstr($buffer, '<span')) && (strstr($buffer, 'titre')))
        {
            $buffer = str_replace('<br />', '|', $buffer);
            #echo $buffer . '<br>';
            $result = explode('|', $buffer);
            #family name variable
            $familyname = $result[0];
            $familyname = str_replace('<span class="titre">', '', $familyname);
            $familyname = trim($familyname);
            $familyname = nameReplace($familyname);
            #first name variable
            $forename = $result[1];
            $forename = nameReplace($forename);
            if(strstr($forename, "RGEN"))
            {
                echo "Forename is ".nameReplace((string)$forename)."<br>";
                $newVar = "JÜRGEN RÜDIGER";
                $newVar = nameReplace($newVar) . "<br>";
                echo "New variable = " . $newVar . "<br>";
            }

            $forename = str_replace("</span>", "", $forename);
            $forename = trim($forename);

            #echo ($namecounter + 1) . ' | ';
            #echo 'Family name : ' . $familyname . ' | Forename is : ' . $forename . '<br>';
            $update_full_file .= $forename . " " . $familyname;
            $firstnames[$namecounter] = $forename;
            $lastnames[$namecounter] = $familyname;
        }#end of if strstr $buffer
        if( (strstr($buffer, 'Age today :')) && (strstr($buffer, '<br')))
        {
            $buffer = str_replace('<br />', '', $buffer);
            $buffer = str_replace('Age today :', '', $buffer);
            $agetoday = $buffer;
            $agetoday = str_replace("<br/>", "", $agetoday);
            $agetoday = trim($agetoday);
            #echo 'Age Today : ' . $agetoday . '<br>';
            $ages[$namecounter] = $agetoday;
            $update_full_file .= ",," . $agetoday;      

            $nextlinenumber = $linenumber + 2;
            $nationalitylinenumbers[$namecounter] = $nextlinenumber;
            #echo $nationcounter . " " .  $nextlinenumber . '<br>';
        }
        if( (strstr($buffer, 'Nationality  :')) )
        {
        #$buffer = str_replace('<br />', '', $buffer);
        #$buffer = str_replace('Age today :', '', $buffer);

        #echo 'Nationality : ' . $buffer . '<br>';
        #echo 'Line number : ' . $linenumber . '<br>';


        }
        if( in_array($linenumber, $nationalitylinenumbers) )
        {
            #echo $nationcounter . $buffer . '<br>';
            #$nationcounter++;
        }

        ###does the current linenumber appear in the array $nationalitylinenumbers?###
        if( in_array($linenumber, $nationalitylinenumbers) )
        {
            $nation = $buffer;  


            $nation = str_replace("<br/>", "", $nation);
            $nation = str_replace("</div>", "", $nation);
            $nation = str_replace(" ", "", $nation);
            #$nation = str_replace(",", "", $nation);
            $nation = str_replace('<divclass="sep">', 'No Country', $nation);

            $nation = trim($nation);
            if(strstr($nation, ','))
            {
                #echo 'Create a duplicate entry';
                $nations = explode(',', $nation);
                $nation = $nations[0];
                $nation = trim($nation);
                $duplicateNation = $nations[1];
                $duplicates[$duplicate_counter] .= $forename . " " .$familyname . ",," . $agetoday . ",,,,,". trim($duplicateNation) .",,,,,\n";
                $duplicate_counter++;
            }
            #$nation = countryMappings($nation);
            ###check to see if entry has 2 countries###

            #echo $nationcounter . $nation . '<br>';
            $nationcounter++;
            $update_full_file .= ",,,,," .$nation. ",,,,,\n";

            $nationalitys[$namecounter] = $nation;
            #echo $buffer.'<br>';
            $namecounter++;

        }

    }#end of while buffer = fgets
}#end of if myfile  
}

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题!

问题是我正在翻译错误的数据。我用'Ü'字符解析并使用:        $ name = str_replace(“Ü”,“U”,$ name); 我将数据导出到文本文件中,发现该字符实际上是以œ而不是Ü导出。我将我的功能修改为如下,它现在有效:        $ name = str_replace(“œ”,“U”,$ name);

再次感谢所有回复的人,所有提示都非常有用。