Change color of output. PHP

2019-09-12 16:32发布

问题:

I created an script that returns interface status from cisco router/switch. Now, i want to change the color of my text based on the word

Ex:

  1. connected=green
  2. notconnected=red

Is this possible ?

part of my script:

 $host = "hostname";
    $name = "my.username\r";
    $pass = "mypassword\r";

    $form = <<<END
    <form method='post' action='' style='margin:auto; width:400px'>
          Adresa IP host : <input type='text' name='host'><br><br>
          <input type='submit' name='submit' value='Connect'>
    </form>
    END;

    echo '<img src="\img\banner.jpg" style="margin:auto; display:block"/><br>';


    echo $form;

    $t = new TELNET();
    if (!empty($_POST)){
       $host = $_POST['host'];
       echo("CONNECT:".$t->Connect($host, $name, $pass)."<br>");
       echo("LOGIN:".(int)$t->LogIn());
       echo("<br>Status Interfete:<br>");
      $interfaces_status = ($t->GetOutputOf("show interface status"));
    foreach ($interfaces_status as $value) {
        echo "$value <br>";

PS: where can i post the script ? I bet there are alot of network engineers interested in this.

回答1:

That's possible with simple inline css:

<?php foreach ($interfaces_status as $value) { ?>
        <span style="color:<?php echo ($value == 'connected') ? 'green' : 'red'; ?>"><?php echo $value; ?><br />
<?php } ?>

But you should rather use classes and declare the styles in an embeded stylesheet.