PHP 搜索文本文档(.txt),抓取整行和行号

发布于 2024-09-16 18:46:34 字数 758 浏览 7 评论 0原文

感谢您花时间阅读本文,无论内容的质量如何,我都会感激每一个回复。 :)

我正在尝试创建一个在文本文件中搜索特定文本的 php 脚本。用户在 HTML 表单中输入特定文本,PHP 脚本应在文本文件中搜索该特定文本。

HTML 表单的输入字段的值为“username”,在 php 文档中,变量“$username”是输入的数据,示例如下:

$username = $_POST['username'];

下面的文本文件名为“rank_order.txt”:

7 奥拉1

13 至尊玩家

13 奥拉1

15 abc123

如果一个人输入 HTML 表单“AULLAH1”,我希望 php 脚本抓取文本文件中的第一个“AULLAH1”条目(例如,它应该抓取第一行而不是第三,因为第一行是包含文本“AULLAH1”的第一个条目)。另外,当一个人输入特定文本时,它不应该简单地抓取文档或文本,它应该抓取整行:“7 AULLAH1”并将其放入 php 变量中,也许类似于“$grabbed” “?如果可能的话。

另外,如果它从第 3 行获取数据,php 变量是否可以代表第 3 行?如果从第 23 行获取数据,那么 php 变量是否可以代表第 23 行(如果从第 1 行获取数据,php 变量可以保存数字 1 吗?等等...)

感谢所有帮助,我期待您的帮助回复;谢谢。 :) 如果我没有清楚地解释任何内容和/或您希望我更详细地解释,请回复。 :)

谢谢。

Thank you for taking the time to read this and I will appreciate every single response no mater the quality of content. :)

I'm trying to create a php script which searches a text file for specific text. A person types in the specific text within a HTML form and the php script should search for that specific text within the text file.

The value of the input field of the HTML form is "username" and within the php document, the variable "$username" is the entered data, example shown below:

$username = $_POST['username'];

The text file below is called "rank_order.txt":

7 AULLAH1

13 SupremeGamer

13 AULLAH1

15 abc123

If a person types into the HTML form "AULLAH1", I'd like the php script to grab the first "AULLAH1" entry in the text file (e.g. It should grab the first line and not the thrid, as the first line is the first entry to contain the text "AULLAH1"). Also with that, when a person types in a specific text, it shouldn't simply grab the document or the text, it should grab the whole line: "7 AULLAH1" and place it into a php variable, maybe something like "$grabbed"? If possible at all.

Also with that, if it grabs the data from line 3, could a php variable represent line 3? If the data is grabbed from line 23, again could a php variable represent line 23 (and if it's grabbed from line 1, could a php variable hold the number 1? etc...)

All assistance is appreciated and I look forward to your replies; thank you. :) If I didn't explain anything clearly and/or you'd like me to explain in more detail, please reply. :)

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等待我真够勒 2024-09-23 18:46:34

fopen、fgets、fclose 将引导您指明方向并介绍文件处理的一般情况。

http://php.net/fgets

尝试在空间上爆炸以获取列。一旦您对此感到满意,就可以考虑将数据存储在数据库中。

fopen, fgets, fclose will lead you in the direction and an intro into file handling in general.

http://php.net/fgets

Try explode on the space to get the columns. Once you're comfortable with that you can consider storing the data in a database.

牵强ㄟ 2024-09-23 18:46:34

几种解决方案:

  1. 遍历文档的行,有很多关于此的操作文章,这将使您能够计算行数。对于每一行,使用正则表达式检查用户输入的字符串

  2. 使用正则表达式搜索文档文本,preg_match() 将返回您正在搜索的字符位置。一旦您知道了要搜索的文本的字符位置,您就可以计算该搜索结果位置之前“\n”(换行符)出现的次数。

  3. 将文本加载到数据库中,然后使用数据库的搜索功能(例如全文搜索或 LIKE %...% 语句)来搜索文本。然后如何计算新行字符 (\n) 的数量直至找到的搜索文本。

Several solutions:

  1. Iterate through the document's lines, there are many how-to articles about this, this will enable you to count the lines. For each line check using regex for the string entered by the user

  2. Search the text of the document using regex, preg_match() will return the character position for what ever it is you are searching for. Once you know the character positions of the text your searching for you can count the number of occurrences of "\n" (new line character) before that search results position.

  3. Load the text into a database, then use the database's search functionality, like Full Text Search or a LIKE %...% statement to search for the text. Then some how count the number of new line characters (\n) up to the search text found.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文