Brief introduction to some functions that may be encountered in PHP network programming
Introduction to PHP network programming functions
1. Fgetcsv function? Parse the read rows and find fields in csv format.
The Fgetcsv function parses the read rows and finds fields in csv format. then, it returns an array containing these fields.
Description of Fgetcsv () function parameters:
Parameters |
Description |
Handle |
Required parameter. Returns the object identification pointer after opening the file. |
Length |
Required parameter. Specifies the maximum number of characters displayed for a row. this value is greater than the maximum number of characters in the csv file. |
Delimiter |
Optional. File Separator |
Enclosure |
Optional. File Separator |
[Example] obtain the number and content of csv fields obtained after each line in the file is separated by commas.
$ Num has $ num csv fields in row $ row:
\ N "; $ row ++; For ($ c = 0; $ c <$ num; $ c ++) {Print $ data {$ c }."
";}} Fcolse ($ handle) ;?>
2. Fgets function? Obtains the content with the specified length in the row where the file pointer is located.
[Syntax ]:
String fgets (inthandle, int length );
The handle parameter is a required parameter. it indicates the ID of the file returned after the fopen () or fsockopen () function is applied to successfully open a file or connect to the server. the length parameter is an optional parameter, string used to specify the maximum number of bytes in a row and return the length-1 bytes. The default value is 1024 bytes.
[Example]: Use the fgets () function to read all the content in the file.
3. Fwrite () function? Write a string to a file
The Fwrite function can write strings to a specified file and specify the size of the written bytes.
[Syntax]
Int fwrite (resource handle, string, intlength)
The handle parameter is a required parameter and specifies the file identifier pointer.
The string parameter is a required parameter and the string to be written to the file.
The length parameter is an optional parameter. it specifies the length of the file to be written. If this parameter is omitted, all data is written by default.
[Example] write a string to a file.
4. Fsockopen function? Open the socket connection of the network
[Syntax]
Int fsockopen (string hostname, int port, interrno, string errstr, int timeout)
Description of parameters of the Fsockopen () function:
Parameters |
Description |
Hostname |
URL and port number |
Port |
Errno |
For error handling, ignore |
Errstr |
Timeout |
Timeout |
After this function, the file pointer will be returned for use by file functions such as fgets (), fgetss (), fputs (), fclose (), and feof.
[Example] open the socket connection with 127.0.0.1.
5. Set_time_limit function? Set the maximum execution time of the script
The default time is 30 s. The parameter is in seconds.
6. Explode function? Use separators to separate strings
The function uses the specified separator to separate strings. it also saves the separated strings into arrays and returns them.
[Syntax] array expode (string separator, string, int limit)
The separator parameter is the delimiter to be used. it can be a single character or a string.
The string parameter indicates the characters to be separated.
The limit parameter indicates the number of elements to be separated. if it is a negative number, all elements except the last limit element are returned.
[Example]
7. Implode function? Concatenates the array content into a string
[Syntax] string implode (string glue, array pieces)
The glue parameter is a connector and the pieces parameter is an array to be merged.
[Example]
8. Parse_url function? Parse url and return array
The function returns an associated array and returns the result to the array, which contains various url components. If one element is missing, an array item is not created for this component.
[Syntax] array parse_url (string url)
The Parse_url () function parses the url string and returns the result to the array.
Elements in the array returned by the Parse_url function:
Element |
Description |
Scheme |
Protocols used by server programs |
Host |
Host |
Port |
Port |
User |
User name |
Pass |
Password |
Path |
Path |
Query |
Query condition, in? After |
Fragment |
After the hash symbol # |
[Example]
”;}?>