Example 11.11.
<html><head><title>Parsing Lines</title></head>
<body bgcolor="lightgreen">
<font face=verdana" size="+2">
<?php
if( ! isset($_POST['submit'])){
1 show_form();
}
else{
2 process_file();
}
3 function show_form(){
?>
<h2>Exploding and Imploding</h2>
<form method=POST
action="<?php echo $_SERVER['PHP_SELF'];?>">
<table cellspacing="0" cellpadding="2">
<tr>
<b> Select a first name from the file.</b>
<td><input type="text" size=30
name="first_name" </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td><input type="submit" name="submit"
</tr><br />
</table>
</form>
<?php
}
4 function process_file(){
$filename="$_SERVER[DOCUMENT_ROOT]/ /mydir/datebook";
5 $lines = file($filename); // Lines of file stored in an
array
6 $first_name=trim($_POST['first_name']);
7 foreach ($lines as $line_value) {
8 $fields=explode(":", $line_value);
9 $fullname=explode(" ", $fields[0]);
$phone=$fields[1];
$address=$fields[2];
$birthday=$fields[3];
$salary=$fields[4];
10 if( strcasecmp($fullname[0],$first_name) == 0 ){
11 $birth=explode("/",$birthday);
12 $newstring=implode("<br
/>",array($fields[0],$phone,
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
$address,"19".$birth[2], '$'.number_format($salary,2)));
13 echo "$newstring<br />";
14 $count++;
echo "<hr />";
}
}
15 if($count==0){
echo "$first_name is not in the file.<br />";
}
}
?>
</b>
</font>
</body>
</html>
Explanation
D
R=#%621"2T8#.%$filename.%"'%2''">*#3%&=#%/2&=%&)%2%7"8#%(288#3%data.file%)*#%8#6#8%
2T)6#%&=#%3)(<5#*&%1))&%)7%&=#%'#16#1%"*%2%3"1#(&)10%(288#3%mydir-
C
R=#%T<"8&Q"*%file()%7<*(&")*%&2N#'%&=#%7"8#*25#%2'%"&'%21><5#*&%+*)&%2%7"8#=2*38#,%
2*3%1#&<1*'%2*%21120%$=#1#%#2(=%8"*#%"'%2*%#8#5#*&%)7%&=#%21120-
O
R=#%foreach%8))/%(0(8#'%)6#1%#2(=%#8#5#*&%)7%&=#%21120%)7%8"*#'.%#;&12(&"*>%&=#%"*3#;%
2*3%&=#%628<#%)7%&=#%8"*#-
@
f0%233"*>%)*#%&)%&=#%628<#%)7%$line_number.%&=#%"*3#;%"*%&=#%21120.%$#%(2*%'&21&%&=#%
()<*&%2&%D-%R="'%628<#%$"88%T#%/82(#3%"*%71)*&%)7%#2(=%8"*#%#6#10%&"5#%&=#%8))/%T)30%"'%
#;#(<-
I
:2(=%8"*#%2*3%"&'%8"*#%*<5T#1%21#%3"'/820#3%2'%'=)$*%"*%`"><1#%DD-DC-
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Figure 11.12. Using the file() function, creating line numbers.
Using explode() and implode()
After you have read in a line from a file or you get input from a file, you might want to break the lines into individual
fields or create a string from an array of input items. This is where the array functions explode() and implode()
can be useful, array functions discussed in Chapter 8, “Arrays.”
Example 11.12 demonstrates how to use these functions. This example uses the text file called datebook.
[6]
Below are
two lines from this file. Notice that the fields are separated by colons.
[6]
The datebook file can be found on the CD in the back of this book.
Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300
Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
%
Example 11.12.
4)3#%!"#$c%
<html><head><title>Parsing Lines</title></head>
<body bgcolor="lightgreen">
<font face=verdana" size="+2">
<?php
if( ! isset($_POST['submit'])){
1 show_form();
}
else{
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
2 process_file();
}
3 function show_form(){
?>
<h2>Exploding and Imploding</h2>
<form method=POST
action="<?php echo $_SERVER['PHP_SELF'];?>">
<table cellspacing="0" cellpadding="2">
<tr>
<b> Select a first name from the file.</b>
<td><input type="text" size=30
name="first_name" </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td><input type="submit" name="submit"
</tr><br />
</table>
</form>
<?php
}
4 function process_file(){
$filename="$_SERVER[DOCUMENT_ROOT]/ /mydir/datebook";
5 $lines = file($filename); // Lines of file stored in an
array
6 $first_name=trim($_POST['first_name']);
7 foreach ($lines as $line_value) {
8 $fields=explode(":", $line_value);
9 $fullname=explode(" ", $fields[0]);
$phone=$fields[1];
$address=$fields[2];
$birthday=$fields[3];
$salary=$fields[4];
10 if( strcasecmp($fullname[0],$first_name) == 0 ){
11 $birth=explode("/",$birthday);
12 $newstring=implode("<br
/>",array($fields[0],$phone,
$address,"19".$birth[2], '$'.number_format($salary,2)));
13 echo "$newstring<br />";
14 $count++;
echo "<hr />";
}
}
15 if($count==0){
echo "$first_name is not in the file.<br />";
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
}
}
?>
</b>
</font>
</body>
</html>
Explanation
D
\7%&=#%7)15%=2'%*)&%T##*%'<T5"&%0#&.%&=#%show_form()%7<*(&")*%$"88%3"'/820%"&-
C
\7%&=#%'<T5"&%T<&&)*%$2'%/1#''#3.%&=#%7)15%=2'%281#230%T##*%7"88#3%)<&.%2*3%*)$%"&%"'%
&"5#%&)%/1)(#''%"&-%R=#%process_file()%7<*(&")*%"'%(288#3-
O
R="'%"'%$=#1#%&=#%show_form()%7<*(&")*%"'%3#(821#3.%$="(=%/1)3<(#'%2%6#10%'"5/8#%
VR9Z%7)15%$"&=%)*#%&#;&%T);%2*3%2%'<T5"&%T<&&)*-%R=#%<'#1%$"88%&0/#%"*%&=#%7"1'&%
*25#%)7%')5#)*#%71)5%2*%#;*28%&#;&%7"8#.%(288#3%datebook-%M##%`"><1#%DD-DO-
@
R="'%"'%$=#1#%&=#%process_file()%7<*(&")*%"'%(288#3-%\&%$"88%1#23%2%7"8#%"*&)%2*%21120%
2*3%T1#2N%#2(=%8"*#%)7%&=#%7"8#%"*&)%7"#83'-%\7%2%8"*#%52&(=#'%&=#%7"1'&%*25#%)7%&=#%
"*/<&%628<#%&0/#3%"*&)%&=#%7)15.%&=2&%32&2%7)1%&=2&%/#1')*%$"88%T#%3"'/820#3-
I
R=#%^V^%file()%7<*(&")*%1#23'%2*%#*&"1#%7"8#%2*3%2''">*'%"&%&)%2*%21120.%$=#1#%#2(=%
8"*#%)7%&=#%7"8#%"'%2*%#8#5#*&%)7%&=#%21120-
K
R=#%628<#%)7%&=#%621"2T8#%(288#3%$first_name%"'%$=2&%&=#%<'#1%&0/#3%"*%&=#%7)15%2*3%
$2'%'#*&%6"2%&=#%POST%5#&=)3%&)%&="'%^V^%/1)>125-%W*0%$="&#'/2(#%"'%'&1"//#3%)<&%
$"&=%&=#%T<"8&Q"*%trim()%7<*(&")*-
E
R=#%foreach%8))/%"&#'%&=1)<>=%#2(=%628<#%"*%&=#%21120%)7%8"*#'%&=2&%$2'%(1#2%
$=#*%&=#%file()%7<*(&")*%1#23%"*%&=#%32&#T))N%7"8#%)*%8"*#%I-
Y
R=#%explode()%7<*(&")*%'/8"&'%</%2%'&1"*>.%$line.%T0%2%'/#("7"#3%3#8"5".%"*%&="'%
(2'#.%T0%2%()8)*-%\&%(1#2&#'%2*%21120%$=#1#%#2(=%)7%&=#%#8#5#*&'%1#/1#'#*&'%&=#%
"*3"6"3<28%7"#83'%&=2&%$#1#%(1#2-
i
R=#%7"1'&%#8#5#*&%)7%&=#%21120%(288#3%$fields[0]%()*&2"*'%2%7"1'&%2*3%82'&%*25#-%R=#%
explode()%7<*(&")*%$"88%(1#2&#%2%&$)Q#8#5#*&%21120.%(288#3%$fullname.%()*'"'&"*>%)7%
2%7"1'&%2*3%82'&%*25#-
DA
R=#%strcasecmp()%7<*(&")*%()5/21#'%&$)%'&1"*>'.%(2'#%"*'#*'"&"6#-%\7%&=#%628<#%&=2&%
(25#%"*%71)5%&=#%7)15.%$first_name.%2*3%&=#%628<#%)7%$fullname[0]%21#%#U<28.%&=#*%
8"*#%DD%"'%#*#3-
DD
R=#%explode()%7<*(&")*%(1#2&#'%&=#%$birth%21120%T0%'/8"&&"*>%$birthday;%7)1%
#;25/8#.%03/16/78.%T0%'82'=#'-
DC.%
DO
R=#%implode()%7<*(&")*%d)"*'%)1%a>8<#'b%&=#%21120%#8#5#*&'%&)>#&=#1%"*&)%)*#%
'&1"*>.%(288#3%"$newstring".%3"'/820#3%)*%8"*#%DO%2*3%'=)$*%"*%`"><1#%DD-D@-
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
DO
'&1"*>.%(288#3%"$newstring".%3"'/820#3%)*%8"*#%DO%2*3%'=)$*%"*%`"><1#%DD-D@-
D@
W%621"2T8#%(288#3%$count%$"88%T#%"*(1#5#*%#2(=%&"5#%&=1)<>=%&="'%8))/%&)%N##/%
&12(N%)7%&=#%*<5T#1%)7%8"*#'%7)<*3%52&(="*>%&=#%*25#%()5/21#3%"*%8"*#%DA-
DI
\7%&=#%628<#%)7%&=#%$count%"'%A.%&=#1#%$#1#%*)%52&(=#'.%2*3%&=#%/1)>125%3"'/820'%2%
5#''2>#%'20"*>%')-
%
Figure 11.13. The HTML form.
%
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Figure 11.14. After the lines in the file have been parsed. Output from Example 11.12.
The readfile() Function—Reading and Writing a File
The readfile() function reads from a file and writes it to the output buffer. It returns the number of bytes read from
the file. If an error occurs, FALSE is returned and an error message is printed.
Format
int readfile ( string filename [, bool use_include_path [,
resource context]] )
%
Example:
readfile("myfile.txt");
Example 11.13.
<html><head><title>Reading and Outputting a File</title></head>
<body bgcolor="lightblue">
<h3>Reading and Outputting a File</h3>
<b>
1 <pre>
<?php
2 $number_of_bytes=@readfile("data.file");
echo "<br />Read $number_of_bytes bytes from the file.<br
/>";
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
?>
</pre>
</b>
</body>
</html>
Explanation
D
R=#%VR9Z%<pre>%&2>%"'%<'#3%=#1#%')%&=2&%$=#*%&=#%7"8#%"'%3"'/820#3.%&=#%
*#$8"*#'%$"88%T#%/1#'#16#3.%2'%'=)$*%"*%`"><1#%DD-DK-
C
R=#%readfile()%7<*(&")*%$"88%1#23%"*%&=#%()*&#*&'%)7%data.file%2*3%'#*3%&=#%
)<&/<&%&)%&=#%&=#%)<&/<&%'&1#25J%7)1%#;25/8#.%T1)$'#1%)1%"*28-%\&%1#&<1*'%
&=#%*<5T#1%)7%T0&#'%1#23-%R=#%7<*(&")*%$2'%(288#3%2'%@readfile()%&)%'<//1#''%
&=#%/1"*&"*>%)7%2*%#11)1%5#''2>#%"7%2*%#11)1%)((<11#3-
Figure 11.15. Reading a file and outputting its contents.
%
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
11.2.8. Opening a File for Writing and Appending
When you open a file for writing, the file is created if it does not exist, and truncated if it does. If you open a file for
appending, the file will not be overwritten if it exists and the data will be appended to the bottom of the file. If it does
not exist, opening for appending creates the file.
To open a file for writing, the fopen() function returns a filehandle to either a text or binary file. Although UNIX and
MacOS do not distinguish between text and binary files, Windows does. If a file is to be shared by multiple operating
systems, it is safer to open it in binary mode in conjunction with one of the other modes. (As of PHP 4.3.2, the default
mode is set to binary for all platforms that distinguish between binary and text mode.)
UNIX and MacOS X represent the end-of-line character with \n, whereas Windows uses \r\n. If you use Windows,
the "t" mode can be used with plain-text files to translate the \n to \r\n. Otherwise, the "b" mode should be used,
which does not interpret data but treats it as just a series of bytes. If the file contents look weird when it is opened or
you have a lot broken images, use the "b" mode.
$handle = fopen("/home/marko/file.txt", "wb"); $handle =
fopen("http://www.ellieq.com/", "w"); $handle =
fopen("ftp://user:password@ellieq.com/myfile.txt", "a");
%
The fwrite() and fputs() Functions
The fwrite() function writes a string text to a file and returns the number of bytes written. An alias for the
fwrite() function is fputs(). It takes two arguments: the filehandle returned by fopen() and an optional length
argument, how many bytes to write to the file. If it fails, fwrite() returns FALSE.
The file_put_contents() Function
The file_put_contents() also writes a string to a file and returns the number of bytes written, but does not
require a filehandle. Otherwise it is the same as fwrite() and fputs().
Format
int fwrite ( filehandle, string, [ int length] )
%
Example:
$bytes=fwrite( $filehandle, "Peter Piper picked a peck of pickled
peppers.\n"); $bytes=fwrite( $filehandle, "Jack in the Beanstalk", 4);
Example 11.14.
%%% <html><head><title>Open a File</title></head>
<body bgcolor="lavender">
<?php
1 $name="Joe Shmoe Jr.";
$address="100 Main St.";
$email="jshmoe@whatever.mil";
$title="Major";
2 $outputstring="$name\t$address\t$email\t$title\n";
3 $filename="$_SERVER[DOCUMENT_ROOT]/ /mydir/info.txt";
4 $filehandle=fopen($filename, "wb");
5 fwrite($filehandle, $outputstring, strlen($outputstring));
fclose($filehandle);
?>
</body>
</html>
(Output: Contents of info.txt)
Joe Shmoe Jr. 100 Main St. jshmoe@whatever.mil Major
%
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Explanation
D
W%'#&%)7%'(2821%621"2T8#'%21#%3#7"*#3.%&=#%"*7)152&")*%&=2&%$"88%T#%$1"&&#*%&)%2%
7"8#-
C
W%'&1"*>%"'%(1#2%$"&=%#2(=%$)13%'#/212%T0%2%&2T-
O
f#(2<'#%&=#%7"8#%5<'&%T#%2%$1"T8#%7"8#.%"&%"'%'&)1#3%)<&'"3#%&=#%'#16#1S'%1))&%
3"1#(&)10-%\&%"'%8)(2%"*%&=#%2%3"1#(&)10%(288#3%mydir.%2*3%&=#%*25#%)7%&=#%7"8#%
"'%info.txt-
@
R=#%7"8#=2*38#%1#&<1*#3%71)5%fopen()%"'%)/#*#3%7)1%$1"&"*>-%R=#%"b"%+T"*210,%
5)3#%"'%1#()55#*3#3%$=#*%<'"*>%'0''%&=2&%3"77#1#*&"2&#%T#&$##*%T"*210%
2*3%&#;&%7"8#'J%&=2&%"'.%B"*3)$'-
I
R=#%fwrite()%7<*(&")*%$1"&#'%&=#%'/#("7"#3%'&1"*>.%$outputstring%&)%&=#%
7"8#=2*38#.%$filehandle-%R=#%*<5T#1%)7%T0&#'%$1"&&#*%21#%1#&<1*#3%T0%&=#%
strlen()%7<*(&")*-
Appending to a File
When a file is opened for appending, the write will start at the end of the file. If the file does not exist, it will be created.
Example 11.15.
4)3#%!"#$c%
<html><head><title>Appending</title></head>
<body bgcolor="lightgreen">
<font face="verdana" size="+1">
<?php
$name="John Doe";
$address="1001 Logic Dr.";
$email="johndoe@place.gov";
$title="VP";
1 $outputstring="$name\t$address\t$email\t$title\n";
$filename="$_SERVER[DOCUMENT_ROOT]/ /mydir/info.txt";
2 $filehandle=fopen($filename, "ab");
3 if( fwrite($filehandle, $outputstring,
strlen($outputstring))==FALSE){
echo "You cannot write to the $filename.<br />";
}
else{
4 $text= file_get_contents("$filename");
echo "<pre>$text</pre>";
}
fclose($filehandle);
?>
</body>
</font>
</html>
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Không có nhận xét nào:
Đăng nhận xét