PHP search and echo specific text(PHP 搜索和回显特定文本)
问题描述
我遇到了问题.我想要做的是让我的 PHP 代码进行搜索,直到找到输入的内容.例如,如果我搜索数字12".我希望它进入一个像下面这样的文件并找到其中包含12"的行.
I'm having a problem. What I want to do is make my PHP code do a search until it finds what was entered. For example if I searched the number "12." I want it to go in a file like the one below and find the line that has "12" in it.
Dark Green = 11 = No = 20,
Light Blue = 12 = No = 20,
Lime Green = 13 = No = 20,
Sensei Gray = 14 = Yes = 0,
在这种情况下,这一行将包含 12:
In that case this line would have the 12 in it:
Light Blue = 12 = No = 20,
接下来我希望代码在找到该行后执行的操作是读取它左侧="符号之前的文本.在这种情况下,我希望我的代码阅读:
Next what I want the code to do after it finds the line is for it to read the text that is before the "=" sign to the left of it. In this case I would want my code to read:
Light Blue
我一直想这样做,任何帮助将不胜感激!
I've always wanted to do this and any help would be HIGHLY appreciated!
推荐答案
试试下面的代码
$string = 'Dark Green = 11 = No = 20,
Light Blue = 12 = No = 20,
Lime Green = 13 = No = 20,
Sensei Gray = 14 = Yes = 0';
$string = explode(',',$string);
foreach($string as $row)
{
preg_match('/^(D+)s=s(d+)s=s(D+)s=s(d+)/', trim($row), $matches);
echo $matches[1];//Dark Green
echo $matches[2];//11
echo $matches[3];//No
echo $matches[4];//20
}
在循环中用于检查要搜索的单词
In the loop use to check the word to search
就这样
if($matches[1] == 'Dark Green')
{
echo $matches[1];
}
或
if($matches[2] == 11)
{
echo $matches[2];
}
(...)要获取文件中的文本,请尝试使用
(...) To get the text in the file try use
$string = file_get_contents('file.txt');
这篇关于PHP 搜索和回显特定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP 搜索和回显特定文本


- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01