Xin chào các bạn. Tôi lại ở đây với một hướng dẫn PHP đơn giản và dễ dàng khác. Trong hướng dẫn này, tôi sẽ chỉ cho bạn cách đọc từng dòng một tệp văn bản. Giống như bất kỳ hướng dẫn nào khác, tôi cũng sẽ chỉ cho bạn mã ví dụ đơn giản để bạn hiểu nó theo cách tốt hơn
Mã ví dụ để đọc từng dòng tệp văn bản trong PHP
Giả sử có một tệp văn bản chứa một tên miền trong mỗi dòng. Ví dụ: giả sử tên tệp văn bản là domainlist. txt chứa một tên miền trên mỗi dòng như bên dưới
codespeedy.com eyeswift.com google.com facebook.com twitter.com linkedin.comBây giờ bên dưới là mã PHP sẽ đọc tệp văn bản và lặp lại từng tên miền từ mỗi dòng bằng cách sử dụng vòng lặp while
<?php if ($file = fopen("domainlist.txt", "r")) { while(!feof($file)) { $textperline = fgets($file); echo $textperline; } fclose($file); } ?>Đoạn mã trên sẽ hiển thị tất cả tên miền trong một dòng
Sắp xếp các tên miền trên mỗi dòng và làm cho nó có thể nhấp vào liên kết
Bây giờ tôi sẽ chỉ cho bạn cách làm cho tên miền có thể nhấp được và sắp xếp nó trên mỗi dòng. Mã PHP tôi sẽ cung cấp cho bạn là bản sửa đổi của mã trước đó. dưới đây là mã
<?php if ($file = fopen("domainlist.txt", "r")) { while(!feof($file)) { $textperline = fgets($file); echo " <a href='//" .$textperline. "'>" . $textperline . "</a><br/><br/> "; } fclose($file); } ?>Đoạn mã PHP vài dòng ở trên sẽ sắp xếp các tên miền trên mỗi dòng với văn bản neo. Kiểm tra mã trên máy chủ của bạn và bạn sẽ thấy mỗi tên miền trên mỗi dòng là một liên kết có thể nhấp được
Một suy nghĩ khác tôi muốn chia sẻ với bạn. Nếu bạn loại bỏ. txt từ tệp văn bản và sử dụng mã mà không có bất kỳ tiện ích mở rộng nào thì nó cũng sẽ hoạt động giống như hiện tại. Trong trường hợp đó, bạn cũng phải xóa phần mở rộng khỏi mã PHP và mã để định vị đường dẫn tệp sẽ là
$file = fopen("domainlist", "r") //the .txt has removed
Cuối cùng, tôi muốn thông báo với bạn rằng hướng dẫn này có thể giúp nhà phát triển theo nhiều cách. Trong nhiều trường hợp, bạn có thể cần lấy dữ liệu từ người dùng. Bạn có thể hướng dẫn người dùng gửi dữ liệu dưới dạng tệp văn bản và thông báo cho người dùng rằng dữ liệu được yêu cầu phải trên cơ sở mỗi dòng
This goes out to Leigh Purdie (5 years ago) and also Dade Brandon (4 months ago)
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:
$ time yes "This is a test line" | head -1000000 | php -r '$fp=fopen("php://stdin","r"); while($line=stream_get_line($fp,65535,"\n")) { 1; } fclose($fp);'
real 0m1.482s
user 0m1.616s
sys 0m0.152s
________số 8_______
real 0m7.281s
user 0m7.392s
sys 0m0.136s
My Results:
$ time yes "This is a test line" | head -1000000 | php -r '$fp=fopen("php://stdin","r"); while($line=stream_get_line($fp,65535,"\n")) { 1; } fclose($fp);'
real 0m0.341s
user 0m0.352s
sys 0m0.148s
________số 8_______
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:0
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:1
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:2
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:3
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:4
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:5
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:6
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:7
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:8
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:9
$ time yes "This is a test line" | head -1000000 | php -r '$fp=fopen("php://stdin","r"); while($line=stream_get_line($fp,65535,"\n")) { 1; } fclose($fp);'0
$ time yes "This is a test line" | head -1000000 | php -r '$fp=fopen("php://stdin","r"); while($line=stream_get_line($fp,65535,"\n")) { 1; } fclose($fp);'1