PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在文本文件中获取用户的价值-PHP大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我要达到什么目的

我想创建一个在文本文件搜索特定单词的函数.要搜索的单词由用户定义,必须以美元开头.

我尝试了什么

我已经搜索过Google和Stackoverflow,但找不到任何东西.
所以我自己尝试了:

function findText($userinput, $fileinput){
    $file = $fopen($fileinput, 'r');
    if(preg_match_all('/\$(\w){1,25}/g', $file, $matches_all)){
        if(strpos($matches_all, $userinput, $matches)){
            return $matches;
        }
    }
}

但这似乎不起作用吗?

基本上

我想这样使用

print_r(findVariable('myword', 'myfile.txt')); //print_r as it's an array
@H_559_0@myfile.txt是:

$myword = also
$myword = and
$myword = this
Hello this is text to ignore
$op = po
Good day
$myword = none

然后它必须输出

Array
(
    [0] => also
    [1] => and
    [2] => this
    [3] => none
)

解决方法:

使用preg_filter

$data = file( $fileinput );    
print_r(preg_filter('#\$' . preg_quote($userinput, "#") . '\s*=\s*#', '', $data));

输出

Array
(
    [0] => also
    [1] => and
    [2] => this
    [6] => none
)

大佬总结

以上是大佬教程为你收集整理的在文本文件中获取用户的价值-PHP全部内容,希望文章能够帮你解决在文本文件中获取用户的价值-PHP所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: