首页 > 解决方案 > 如何使用 fopen 和 fget 读取文件特定文本

问题描述

我有一个将凭据存储到服务器的 config.txt 文件。我需要从特定点读取凭据在此处输入图像描述

从上面的图片中,我只需要在两个 :: 位置之后读取三个变量

标签: php

解决方案


只需使用正则表达式:

<?php
$pattern = '/server_name::(.*)$/';
$file = '/Your/file/path/here';
preg_match($pattern, file_get_contents($file), $matches);
print_r($matches);
?>

第一个匹配项应该是服务器名称:$matches[0]


推荐阅读