[英]PHP Function running locally but not within a loop
I have this function and statements running fine in my page: $name = "JÜRGEN RÜDIGER HERMANN"; 我的页面中运行了该函数,并且语句运行良好:$ 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>";
Everything about works fine, the characters are changed to acceptable characters for upload into a progress DB. 一切正常,将字符更改为可接受的字符,以上传到进度数据库。 But later in the page I'm looping through data and taking strings from a parsed HTML file:
但是在页面的后面,我将遍历数据并从已解析的HTML文件中获取字符串:
$familyname = nameReplace($familyname);
The above familyname nameReplace() fails. 上面的家族名称nameReplace()失败。 If I echo out just family name it echoes out the string.
如果我只呼出姓氏,它就会呼出字符串。 If I echo out nameReplace($familyname) it returns blank or false.
如果我呼出nameReplace($ familyname),则返回空白或false。
Any ideas? 有任何想法吗? Do functions have a specific scope?
功能是否有特定范围?
Thanks, Stephen. 谢谢,斯蒂芬。
Below is the code for the loop as requested: 以下是请求的循环代码:
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
}
I managed to fix this! 我设法解决了这个问题!
The issue was I was translating the wrong data. 问题是我正在翻译错误的数据。 I parsed in the 'Ü' character and was using: $name = str_replace("Ü", "U", $name);
我解析了“Ü”字符,并使用了:$ name = str_replace(“Ü”,“ U”,$ name); I exported the data to a text file and found that the character is actually exporting as Ãœ instead of Ü.
我将数据导出到一个文本文件,发现该字符实际上是导出为Ãœ而不是Ü。 I amended my function to be as below and it now works: $name = str_replace("Ãœ", "U", $name);
我将函数修改如下,现在可以使用:$ name = str_replace(“Ô,“ U”,$ name);
Thanks again to all who responded, all the tips were very helpful. 再次感谢所有回答,所有提示都非常有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.