我正在编写一个从文件中读取并从该文件中搜索学生学位的php简单代码。例如在我的文件中我有:
123 20
456 18
789 19
我希望以示例' 123'这个代码的警告框中的程度:
<?php
function show_degree($str) {
echo '<script type="text/javascript">alert("' . $str . '")</script>';
}
$given_number="123";
$myfile = fopen("degree/dm.txt", "r");
while (($line = fgets($myfile)) !== false) {
list($number, $degree) = explode("\t", $line);
if($number==$given_number) {
echo $degree;
show_degree( 'Hello:\\n\\nYour Degree:\\n'.$degree );
}
}
fclose($myfile);
?>
我该怎么办? tnx很多
答案 0 :(得分:0)
尝试使用此功能:您的explode("\t", $line);
未向您提供所需的结果。
<?php
function show_degree($str)
{
echo '<script type="text/javascript">alert("' . $str . '")</script>';
}
$given_number="123";
$myfile = fopen("degree/dm.txt", "r");
while (($line = fgets($myfile)) !== false)
{
//echo $line;
list($number, $degree) = explode(" ",preg_replace("/\s+/", " ", $line));// change here
if($number==$given_number)
{
// echo $degree;
show_degree( 'Hello:\\n\\nYour Degree:\\n'.$degree );
}
}
fclose($myfile);
?>
答案 1 :(得分:0)
我用这段代码解决了自己的问题:
<?php
function show_degree($str,$deg) {
$deg=""+$deg;
echo '<script type="text/javascript">alert("' . $str .$deg. '")</script>';
}
$given_number="90222179";
$myfile = fopen("degree/dm.txt", "r");
while (($line = fgets($myfile)) !== false) {
list($number, $degree) = explode("\t", $line);
if($number==$given_number) {
echo $degree;
show_degree( "Hello\\n\\nYour Degree:\\n",$degree );
}
}
fclose($myfile);
?>