1

从主题本身来看,我需要将一个字符串与文本区域内的一行文本进行比较。

这是我从我的 linux 服务器的 php 得到的输出,我有一个程序可以打印可用的设备,我把它放在一个 textarea 中。

Device --------------      |        NAme       ------------------- |        Status  

/dev/ttyS0-----------| Profilic |-------------------|Available

/dev/ttyUSB0 -------| Test | ---------------------|Busy

现在我有一系列设备..

$devices = array("/dev/ttyS0", "/dev/ttyUSB0", "/dev/ttyUSB1");

现在,如果 textarea 中存在以下设备,我如何比较我的字符串数组?

假设如果/dev/ttyS0在 textarea 中找到,则返回 true,因为我的字符串数组中有 /dev/ttyS0。

示例代码我如何从 linux 获取输出到 php。

echo "<textarea>";   
echo stream_get_contents($pipes[1]); 
echo "</textarea>";

我想要发生的事情。(样机代码)

if(/dev/ttyS0 == in the textarea){
  enable this part of code
}

if(/dev/ttyUSB0 == in the textarea){
  enable this part of code
}

and so on....

我怎样才能做到这一点?..

4

2 回答 2

1

假设您的设备描述符应按上述格式出现在 textarea 中的行首,您可以遍历这些行并查找strpos($line, $device) === 0.

$lines = explode("\n", $teextarea_content);
// loop over array of device descriptors
// and make an array of those found in the textarea
$found_devices = array();
foreach ($devices as $device) {
  // Iterate over lines in the textarea
  foreach ($lines as $line) {
    if (strpos($line, $device) === 0) {
      // And add the device to your array if found, then break
      // out of the inner loop
      $found_devices[] = $device;
      break;
    }
  } 
}
// These are the devices you found...
var_dump($found_devices);

// Finally, enable your blocks.
if (in_array("/dev/ttyUSB0", $found_devices)) {
   // enable for /dev/ttyUSB0
}
// do the same for your other devices as necessary

// OR... You could use a fancy switch in a loop to act on each of the found devices
// Useful if two or more of them require the same action.
foreach ($found_devices as $fd) {
  switch($fd) {
    case '/dev/ttyUSB0':
      // stuff for this device
      break;
    case '/dev/ttyS0':
      // stuff for this device
      break;
    // These two need the same action so use a fallthrough
    case '/dev/ttyS1':
    case '/dev/ttyS2':
      // Stuff for these two...
      break;
  }
}
于 2012-10-03T01:15:23.450 回答
0

您很可能希望使用 AJAX 来执行此操作... JQuery 使 AJAX 变得非常简单。

但是您的 PHP 会希望看起来像...

<?php
// Already assuming you have filled out your array with devices...
$dev = $_POST["device"];


// This will loop through all of your devices and check if one matched the input.
foreach($devices as $device) {
    if ($device == $dev) {
        // Whatever you want to do if the device matches.
    }
}
?>

干杯

于 2012-10-03T01:14:57.647 回答