gpt4 book ai didi

PHP nextLine 带字符串

转载 作者:行者123 更新时间:2023-12-04 06:06:58 27 4
gpt4 key购买 nike

我在我的网站上写了一个 php 搜索脚本来检查一个包是否存在于文本文件中(我使用了 file_get_contents 。)它需要用户输入的内容并将其与字符串 'BUILD:' 结合起来,下面是一个文件的例子,我将在它后面的段落中解释:

BUILD: packageA
URL: www.package-a.com

BUILD: packageB
URL: www.package-b.com

BUILD: packageC
URL: www.package-c.com

因此,如果用户要搜索“packageB”,它将与“BUILD:”结合使用,使我正在测试的变量具有值:“BUILD:packageB”。我有一个条件说明该字符串是否存在;一切都在这方面运作良好。

我的问题/问题是如何列出 URL: 其下方的行并带有 echo ?我一直在用 strlen() 测试一些想法我似乎无法得到它,文本文件有不同的输入字符串。有没有办法让 PHP 强制执行 "nextLine()"如果有这样的事情...... PHP 是否识别返回分隔符?

谢谢!

最佳答案

// The string you are searching for
$package = 'packageB';

// Get the file into an array
$data = file('myfile.txt');

// Loop the data
for ($i = 0, $found = FALSE; isset($data[$i]); $i++) {
if (trim($data[$i]) == "BUILD: $package") { // We found the one we're looking for
echo trim($data[++$i]); // Echo the next line
$found = TRUE;
break; // Stop looping
}
}

if ($found) {
echo "<br />\nI found the URL on line $i";
} else {
echo "I didn't find it!";
}

file() 以数组的形式获取文件数据,其中每个元素都是文件的一行,这与 file_get_contents() 等单个字符串不同返回。

关于PHP nextLine 带字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8243622/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com