Some of the functions encountered in PHP network programming are briefly introduced

Source: Internet
Author: User

Introduction to PHP Network programming functions

1. Fgetcsv function-Parses a read-in row and finds fields in CSV format.

The Fgetcsv function resolves a read-in row and finds a field in CSV format, and then returns an array containing those fields.

Description of the Fgetcsv () function parameter:

Parameters

Description

Handle

Required parameters. Return file ID pointer after opening file

Length

Required parameters. Specifies the maximum number of characters that a row displays, which is greater than the number of characters in the longest line of the CSV file.

Delimiter

Optional parameters. The delimiter for the file

Enclosure

Optional parameters. The delimiter for the file

Example gets the number and contents of the CSV field that each row in the file gets separated by commas.

<? PHP      $row = 1;      $handle = fopen ("Test.csv", "R");      while ($data = Fgetcsv ($handle, +, ",")) {             $num = count ($data);             Print <p> $num has $num csv field on line $row: </br>\n ";             $row + +;             for ($c = 0; $c < $num; $c + +) {              print$data{$c}. " <br> ";}} Fcolse ($handle);? >

2. Fgets function – Gets the contents of the specified length of the line where the file pointer resides

"Syntax":

String fgets (inthandle,int length);

Parameter handle is a required parameter, which refers to the file identity that is returned when the file is successfully opened or connected to the server by applying the fopen () or fsockopen () function; The parameter length is an optional parameter that specifies the maximum number of bytes to read and returns the string of bytes that are length-1. Omit the default of 1024 bytes.

Example: Apply the Fgets () function to read the entire contents of the file.

<? PHP     $handle = fopen ("Test.txt", "R");     while (!feof ($handle)) {            $buffer = fgets ("$handle, 2048");            Echo $buffer;} Fclose ($handle);? >

3. Fwrite () function-writes a string to a file

The Fwrite function writes a string to the specified file and can specify the size of the write bytes.

Syntax

Int fwrite (Resource handle,string string, intlength)

The parameter handle is a required parameter, and the file identifies the pointer.

The argument string is a required parameter, and the string to write to the file.

The parameter length is an optional parameter, writing to the file, omitting all writes by default.

"Example" writes a string to a file.

<? PHP      $fp = fopen ("Test.txt", "w+");      $str = "Veaglefly";      If (!fwrite ($fp, $str)) {             Echo "file written successfully!"} else{       Echo "File write Failed! ":}?>

4. Fsockopen function-Open the network socket connection

Syntax

Int fsockopen (String hostname,int port,interrno,string errstr,int Timeout)

The parameter description of the Fsockopen () function:

Parameters

Description

Hostname

URLs and port numbers

Port

Errno

Used as error handling, can be ignored

Errstr

Timeout

Timeout period

After this function, a file pointer is returned for use by Fgets (), FGETSS (), fputs (), fclose (), feof (), and other file functions.

"Example" opens the socket connection to 127.0.0.1.

<? PHP      $fp = Fsockopen ("127.0.0.1");      If (! $fp) {             Echo "Connection failed";} else{       Echo "Connection succeeded";} Fclose ($FP);? >


5. Set_time_limit function-Set the maximum execution time of the script

The default time is 30s, and the parameter is in seconds.

6. Explode function-separating strings with separators

The function is to delimit the string with the specified delimiter, and to save the delimited string to the array and return it.

"Syntax" Array expode (string separator,string string,int limit)

The parameter separator is the delimiter to use, either a single character or a string.

The argument string represents the character to be delimited.

The parameter limit indicates the number to be delimited, and if it is negative, all elements except the last limit element are returned.

Example

<? PHP      $strings = "apples, bananas, pears";      $string = Explode (",", $string);      Print_r ($string);      Echo$string[0];? >

7. Implode function-concatenate array contents into a string

"Syntax" string implode (string Glue,array pieces)

The parameter glue is a connector, and the parameter pieces is an array to be merged.

Example

<? PHP      $str = "php*jsp*asp";      $str _arr=explode ("*", $str);      $array =implode ("*", $str _arr);      Echo$array;? >

8.parse_url Function-parses the URL and returns the array

The function returns an associative array and returns the results to the array, containing the various components of the URL. If one is missing, an array entry is not created for this component.

"Syntax" array parse_url (string url)

The Parse_url () function is specifically parsing the URL string and returning the result to the array.

The Parse_url function returns an array of elements that contain:

Elements

Description

Scheme

protocol used by the server program

Host

Host

Port

Port

User

User name

Pass

Password

Path

Path

Query

Query criteria, in? After

Fragment

After the hash symbol #

Example

<? PHP      $data = "HTTP://www.mingrisoft.com/mrbbs/index.php?id=8";      $str _url= Parse_url ($data);      while ($array =each ($str _url)) {             Echo "url[". $array ["Key"]. "] = ". $array [" Value "]." <br> ";}? >

Some of the functions encountered in PHP network programming are briefly introduced

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.