如何在PHP中选择输出

时间:2018-07-16 05:46:55

标签: php

我想选择要显示的输出。我将以下代码组成

<?php
function getURL($u){
        $ops = array(
          'http'=>array(
            'method'=>"GET",
            'header'=>"Accept: text/html\r\n" .
                      "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0\r\n"
          )
        );
        $co = stream_context_create($ops);
        $r =  $text . file_get_contents('http://' . $u, false, $co);
        return $r != false ? $r : "";

    }
    function GetStringBetween($string, $start, $finish){
        $string = " ".$string;
        $position = strpos($string, $start);
        if ($position == 0) return "";
        $position += strlen($start);
        $length = strpos($string, $finish, $position) - $position;
        return substr($string, $position, $length);
    }
  $url = (!isset($_GET["f"])) ? "chosenoutput" : htmlspecialchars($_GET["f"]);
  $A = GetStringBetween(getURL("blahblah.com"),'start', 'stop');
  $B = GetStringBetween(getURL("blahblah2.com"),'start', 'stop');


?>
<?= trim(url, "\r\n\t ")?>

所以我的目标是能够执行mydomain.com/file.php?f=A,并且将输出$A的字符串,如果我执行了mydomain.com/file.php?f=B,则将输出$ B的字符串。

1 个答案:

答案 0 :(得分:0)

简单到if f = A, show A, else if f = B, show B

if (isset($_GET['f']) && $_GET['f'] == 'A')
    echo $A;
elseif (isset($_GET['f']) && $_GET['f'] == 'B')
    echo $B;