/**
*
* @param array $arrConfig
* @return $config
*/
function ParseConfig( $arrConfig ) {
$config = array();
foreach( $arrConfig as $line ) {
$line = trim($line);
if( $line != "" && $line[0] != "#" ) {
$arrLine = explode( "=",$line );
$config[$arrLine[0]] = ( count($arrLine) > 1 ? $arrLine[1] : true );
}
}
return $config;
}
exec( 'cat '. RASPI_DNSMASQ_CONFIG, $return );
$conf = ParseConfig($return);
$arrRange = explode( ",", $conf['dhcp-range'] );
$RangeStart = $arrRange[0];
$RangeEnd = $arrRange[1];
$RangeMask = $arrRange[2];
preg_match( '/([0-9]*)([a-z])/i', $arrRange[3], $arrRangeLeaseTime );这将从文件中提取数据,并将数字添加到一个值中
我想在同一个文件中做同样的事情,但是从以dhcp-option=6开头的行中提取数字,
任何帮助都会很好,我是超级新人,抱歉
exec( 'cat '. RASPI_DNSMASQ_CONFIG, $return1 );
$conf1 = ParseConfig($return1);
$arrDNS = explode( ",", $conf1['dhcp-option=6,'] );
$Dns = $arrDNS[0];这是我对它的尝试:( $Dns似乎是空白的
在文件中它
interface=wlan1
dhcp-range=192.168.110.20,192.168.110.120,255.255.255.0,12h
dhcp-option=6,8.8.8.8,8.8.4.4这就是我试图拉出字符串8.8.8.8,8.8.4.4的地方
<div class="row">
<div class="form-group col-md-4">
<label for="code">Enter DNS adress seprate with , </label>
<input type="text" class="form-control" name="Dns" value="<?php echo $Dns; ?>" />
</div>
</div>发布于 2020-05-23 01:02:20
$lines = file('/etc/dnsmasq.conf');
$DNSTEST = $lines[2];
$Dns = substr($DNSTEST, 14);我将此作为一个非常简单的方法来完成它,前14个字符永远不会改变
<div class="row">
<div class="form-group col-md-4">
<label for="code">Enter DNS adress seprate with , </label>
<input type="text" class="form-control" name="Dns" value="<?php echo $Dns; ?>" />
</div>
</div>我现在唯一的问题是我宁愿使用". RASPI_DNSMASQ_CONFIG“而不是完整的文件路径,但似乎不能让它工作,让我知道如果你有任何想法
发布于 2020-05-22 18:23:26
首先,你不需要使用cat来读取PHP中的文件,它有内置的函数。
$return = file(RASPI_DNSMASQ_CONFIG, FILE_IGNORE_NEW_LINE);ParseConfig()使用=之前的关键字作为$conf数组的键。所以你想要的选项是在$conf['dhcp-option']中。
$arrDNS = explode(',', $conf['dhcp-option']);
if ($arrDNS[0] == '6') {
$dns = implode(',', array_splice($arrDNS, 1));
}请注意,由于ParseConfig()的工作方式,您将无法解析不同的dhcp-option=X值。数组键必须是唯一的,不能有多个$conf['dhcp-option']。它将始终是文件中的最后一个。
https://stackoverflow.com/questions/61951908
复制相似问题