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 搜索和回显特定文本
基础教程推荐
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01