簡體   English   中英

如何從此函數中獲取特定文本?

[英]How to get a certain text from this function?

我想知道如何獲取文本“ Physical Address. . . . . . . . . : xx-xx-xx-xx-xx-xx Physical Address. . . . . . . . . : xx-xx-xx-xx-xx-xx Physical Address. . . . . . . . . : xx-xx-xx-xx-xx-xx $cmd = system('ipconfig /all') Physical Address. . . . . . . . . : xx-xx-xx-xx-xx-xx

當我寫$ cmd = system('ipconfig /all') ,屏幕上會顯示一個文本。 即使沒有回顯$cmd 如何刪除文本,但部分文本。 我想在文字Physical Address之后輸入38 chars

注意:更重要的是擺脫顯示的system('ipconfig /all')文本system('ipconfig /all')但仍將其包含在變量中

除了Mohammad的答案,您還可以使用輸出緩沖。

就像是:

 ob_start();
 system('ipconfig /all');

 $output = ob_get_clean();

 print_r( $output);

應該捕獲命令返回的所有內容。 然后,您可以按自己喜歡的方式操縱它。

您可以將第二個參數發送到系統功能以獲取命令的輸出:

system('ipconfig /all', $output); //$output is result of command 

同樣,您也可以使用shell_exec函數僅返回輸出,並且默認情況下不回顯,如下所示:

$output = shell_exec('ipconfig /all'); 

$val = shell_exec('ipconfig /all'); echo $val;

您需要測試一下,因為我不完全知道您的IPCONFIG結果(因操作系統而異),但這與您想要的非常接近:

<?php
$command = "ipconfig /all";
$output = shell_exec($command);
$output = explode(PHP_EOL, $output);
$output = $output[0];
$result = explode("Physical Address",$output);
echo substr($result[1],20,17);
?>

請記住,您可能需要更改echo substr($result[1],20,15); 滿足您的需求,因為它取決於您擁有多少個網卡等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM