php的 UTF-8 With BOM
今天再用php实现成员信息录入功能实现,当进行提交时,将以表格形式进行显示所有录入的成员的信息:
成员录入界面:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>成员信息录入表</title>
</head>
<body>
<form action="test.php" method="post">
<table>
<tr>
<td>姓名</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>邮箱</td>
<td><input type="email" name="email"></td>
</tr>
<tr>
<td>网址</td>
<td><input type="url" name="url"></td>
</tr>
<tr>
<td></td>
<td><input type="submit"></td>
</tr>
</table>
</form>
</body>
</html>
成员信息显示
<?php
$username=$_POST['username'];
$age=$_POST['age'];
$email=$_POST['email'];
$url=$_POST['url'];
$file="data.txt";
$line = count(file($file));
echo "获取文件的行数是:".$line;
echo $username.$age.$email.$url;
echo "<br>";
$content=($line+1)."|".$username."|".$age."|".$email."|".$url."\r\n";
echo $content;
$fp=fopen($file, "a");
fwrite($fp, $content);
fclose($fp);
$contents=file_get_contents($_SERVER['DOCUMENT_ROOT']."/data.txt");
$lines=explode("\n", $contents);
$data=array();
echo "<pre>";
var_dump($lines);
foreach ($lines as $items) {
# code...
if (!$items) {
# code...
continue;
}
$cols=explode("|",$items);
$data[]=$cols;
}
var_dump($data);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>全体成员信息表</title>
<style>
table{
width: 500px;
}
tr{
height: 50px;
}
td{
text-align: center;
}
td>a{
text-decoration: none;
color: red;
}
</style>
</head>
<body>
<h1>全体成员信息表</h1>
<table border="1" cellpadding="0" cellspacing="0" >
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>邮箱</th>
<th>网址</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $line): ?>
<tr>
<?php foreach ($line as $col): ?>
<?php $col=trim($col); ?>
<?php if (strpos($col,"http://")===0): ?>
<td><a href="<?php echo strtolower($col);?>"><?php echo substr($col, 7); ?></a></td>
<?php else: ?>
<td><?php echo $col;?></td>
<?php endif ?>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</tbody>
</table>
</body>
</html>
<!-- $content=file_get_contents($_SERVER['DOCUMENT_ROOT']."/data.#txt");
#var_dump($content);
#$new_array=explode("\n", $content);
#echo "<pre>";
#print_r($new_array);
#echo "<table border=1>";
#foreach ($new_array as $key => $value) {
# # code...
# echo "<tr>";
# $td_arr=explode("|", $value);
# print_r($td_arr);
# foreach ($td_arr as $key => $value) {
#
# $new_str=strtolower($value);
# echo "<td>";
# if($key==4){
# $link=substr($new_str, 7);
# echo "<a href=".$new_str.">".$link."</a>";
# }
# else{
# echo $new_str."</td>";
# }
# }
# echo "</tr>";
#}
#echo "</table>";
# -->
<?php
$username=$_POST['username'];
$age=$_POST['age'];
$email=$_POST['email'];
$url=$_POST['url'];
$file="data.txt";
$line = count(file($file));
echo "获取文件的行数是:".$line;
echo $username.$age.$email.$url;
echo "<br>";
$content=($line+1)."|".$username."|".$age."|".$email."|".$url."\r\n";
echo $content;
$fp=fopen($file, "a");
fwrite($fp, $content);
fclose($fp);
$contents=file_get_contents($_SERVER['DOCUMENT_ROOT']."/data.txt");
$lines=explode("\n", $contents);
$data=array();
echo "<pre>";
var_dump($lines);
foreach ($lines as $items) {
# code...
if (!$items) {
# code...
continue;
}
$cols=explode("|",$items);
$data[]=$cols;
}
var_dump($data);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>全体成员信息表</title>
<style>
table{
width: 500px;
}
tr{
height: 50px;
}
td{
text-align: center;
}
td>a{
text-decoration: none;
color: red;
}
</style>
</head>
<body>
<h1>全体成员信息表</h1>
<table border="1" cellpadding="0" cellspacing="0" >
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>邮箱</th>
<th>网址</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $line): ?>
<tr>
<?php $num=0; ?>
<?php foreach ($line as $col): ?>
<?php $num++; echo $num;?>
<?php $col=trim($col); ?>
<?php if($num==1): $col=str_pad($col, 3,"0",STR_PAD_LEFT); ?>
<?php endif ?>
<?php if (strpos($col,"http://")===0): ?>
<td><a href="<?php echo strtolower($col);?>"><?php echo substr($col, 7); ?></a></td>
<?php else: ?>
<td><?php echo $col;?></td>
<?php endif ?>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</tbody>
</table>
</body>
</html>
如上面所示的一样,基本功能已经实现,成员的信息已经进行录入,并且写入到文本文档data.txt中,当想要进行对其编号栏所有编号不满三位的进行左侧补零操作时候遇到了问题:
发现第一个编号始终是设置不上,于是我就通过输出它的字符串长度,发现既然是5个,明明是一个的啊,后来通过百度,发现文本文档在windows系统的时候,默认开头会有看不见的字符,这种现象就是utf-8 with bom,后来进行百度找到了解决方法,就是在你该文件的文件夹内,建立一个php文件,将下面代码进行复制,并进行执行,就可以清除掉
<?php
// 设定你要清除BOM的根目录(会自动扫描所有子目录和文件)
$HOME = dirname(__FILE__);
// 如果是Windows系统,修改为:$WIN = 1;
$WIN = 0;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>UTF8 BOM 清除器</title>
<style>
body { font-size: 10px; font-family: Arial, Helvetica, sans-serif; background: #FFF; color: #000; }
.FOUND { color: #F30; font-size: 14px; font-weight: bold; }
</style>
</head>
<body>
<?php
$BOMBED = array();
RecursiveFolder($HOME);
echo '<h2>These files had UTF8 BOM, but i cleaned them:</h2><p class="FOUND">';
foreach ($BOMBED as $utf) { echo $utf ."<br />\n"; }
echo '</p>';
// 递归扫描
function RecursiveFolder($sHOME) {
global $BOMBED, $WIN;
$win32 = ($WIN == 1) ? "\\" : "/";
$folder = dir($sHOME);
$foundfolders = array();
while ($file = $folder->read()) {
if($file != "." and $file != "..") {
if(filetype($sHOME . $win32 . $file) == "dir"){
$foundfolders[count($foundfolders)] = $sHOME . $win32 . $file;
} else {
$content = file_get_contents($sHOME . $win32 . $file);
$BOM = SearchBOM($content);
if ($BOM) {
$BOMBED[count($BOMBED)] = $sHOME . $win32 . $file;
// 移出BOM信息
$content = substr($content,3);
// 写回到原始文件
file_put_contents($sHOME . $win32 . $file, $content);
}
}
}
}
$folder->close();
if(count($foundfolders) > 0) {
foreach ($foundfolders as $folder) {
RecursiveFolder($folder, $win32);
}
}
}
// 搜索当前文件是否有BOM
function SearchBOM($string) {
if(substr($string,0,3) == pack("CCC",0xef,0xbb,0xbf)) return true;
return false;
}
?>
</body>
</html>
最后重新进入成员信息表进行显示发现问题已经得到解决
通过百度将问题进行解决,于是上传到博客,希望对以后有问题的朋友,可以得到类似的问题帮助。