php - 从txt文件中获取特定数据
问题描述
我有 txt 文件,我需要在循环中回显特定数据。
假设我的 txt 文件名是这样的:myfile.txt
里面的结构是这样的:
etc="Orange" src="stack1"
etc="Blue" src="stack2"
etc="Green" src="stack3"
etc="Red" src="stack4"
如何在 PHP 中回显这些值:橙色、蓝色、绿色、红色?
解决方案
您可以preg_match_all
为此使用:
<?php
# get your text
$txt = file_get_contents('your_text_file.txt');
# match against etc="" (gets val inside the quotes)
preg_match_all('/etc="([^"]+)"/', $txt, $matches);
# actual values = $matches[1]
$values = $matches[1];
echo '<pre>'. print_r($values, 1) .'</pre>';
推荐阅读
- node.js - 如何将重聚合的结果通过管道传输到可写流?(节点)
- java - java.lang.NoSuchMethodError:org.openqa.selenium.support.ui.WebDriverWait.until
- date - 如何在颤振中添加年份范围验证
- regex - 使用正则表达式区分两个相似的文本
- java - 具有整数资源的 switch case
- wpf - Telerik 自定义样式在应用于 RadButton 时不反映
- python - 如何检查csv文件中的句子是否存在于另一个csv文件的列中
- javascript - 将 Styled-Map 与 PolishedJS 一起使用
- java - 无法连接到 MQTT 服务器:“原因代码:5 - 无权执行请求的操作”
- java - 在 ArrayList 中搜索字符串实例