This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
uphp:functions:fgets [2021/04/07 08:40] admin |
uphp:functions:fgets [2021/09/13 05:57] (current) |
||
---|---|---|---|
Line 3: | Line 3: | ||
< | < | ||
- | Get a string of arbitrary length | + | Return |
- | ====Library==== | + | |
- | + | ||
- | / | + | |
====Description==== | ====Description==== | ||
<well size=" | <well size=" | ||
- | <span style=" | + | <span style=" |
- | <span style=" | + | <span style=" |
<span style=" | <span style=" | ||
<span style=" | <span style=" | ||
- | ) | + | <span style=" |
+ | <span style=" | ||
+ | <span style=" | ||
+ | <span style=" | ||
</ | </ | ||
- | Since uPHP has a maximum length of 255 bytes for strings read from disk, | + | This function works with different resource types. If no size is specified, it will keep reading |
- | this function checks for the condition and keeps reading till the end of | + | |
- | the string. | + | **Note:** The maximum length of a returned string in [[hardware: |
====Parameters==== | ====Parameters==== | ||
Line 26: | Line 26: | ||
< | < | ||
+ | < | ||
====Return Values==== | ====Return Values==== | ||
- | * < | + | * < |
+ | * Empty < | ||
+ | * < | ||
====Example==== | ====Example==== | ||
+ | |||
+ | ===Open a TCP connection to google.com and print the header for a search for the word ' | ||
<code php> | <code php> | ||
< | < | ||
- | | + | $f=fsockopen("www.google.com",80,10); |
- | + | ||
- | | + | |
- | | + | |
if ($f) { | if ($f) { | ||
- | $line=0; | + | |
- | | + | |
- | $st=fgets_long($f); | + | |
- | print(" | + | $out = $out." |
+ | fwrite($f,$out); | ||
+ | print($out); | ||
+ | // wait for and receive the header (multiple lines): | ||
+ | $done=0; | ||
+ | while (!$done) { | ||
+ | $str=fgets($f); | ||
+ | print($str+"\r\n"); | ||
+ | if (strlen($str)==0) $done=1; | ||
} | } | ||
fclose($f); | fclose($f); | ||
} | } | ||
- | | ||
?></ | ?></ | ||
</ | </ | ||
+ | ====Also See==== | ||
+ | |||
+ | [[strlen()]] - Return the length of a < | ||
+ | |||
+ | [[fopen()]] - Open a file for reading or writing | ||
+ | |||
+ | [[fseropen()]] - Open the serial port at the specified baud rate with optional parameters | ||
+ | |||
+ | [[f485open()]] - Open the RS-485 port at the specified baud rate and parity | ||
+ | |||
+ | [[fsockopen()]] - Open an internet socket connection with optional timeout | ||
+ | |||
+ | [[fread()]] - Read bytes from a file, stream or socket | ||
+ | |||
+ | [[fwrite()]] - Write data to a file, stream or socket | ||
+ | |||
+ | [[feof()]] - Test if no more data is available in a file, stream or socket | ||
+ | |||
+ | [[filesize()]] - Return the size of a file, or the number of unread bytes in a stream or socket | ||
+ | |||
+ | [[fseek()]] - Position the file pointer in an open file | ||
+ | |||
+ | [[ftell()]] - Return the current position of a file read/write pointer | ||
+ | [[fclose()]] - Close a file, stream or socket | ||