在我无法在HTML中回显的函数中具有变量

问题描述 投票:0回答:1

我有一个带有“提交”按钮的表单,单击该表单将调用display()函数。

display()是一个函数,其中包括另一个.php文件,在该文件中,我具有可以对ID进行不同转换的功能。

我现在的问题是,我想将已转换的ID“回显”到我的html中,由于转换的变量在函数内部,所以我不能这样做。

[当前,我正在执行以下操作,我将转换功能(具有要回显到HTML的变量)放在一部分” php标记”中,然后在HTML中包含” php标记,回显”。] >

但是可悲的是,我尝试了很多事情,将变量设置为global,尝试在其他位置定义它们,但是变量将保持未定义状态。

基本上,我是在问,是在我做错了什么,还是有更好/更合理的方法这样做,因此我可以调用这些变量。(请记住我每次都需要它们,因为在提交表单时正在提交URL。)

这是我的代码(index.php)

            <head>
                <meta charset="UTF-8">
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
                <meta http-equiv="X-UA-Compatible" content="ie=edge">
                <title>STEAM CONV</title>
                <link rel="stylesheet" href="style.css">
            </head>

            <body>
                <form method="post">
                    <input type="text" name="profile_url">
                    <input type="submit" value="click" name="submit">
                </form>

                <h1><?php echo $userid;   ?></h1>

               <img onerror="this.style.display='none'" src="<?php

                    require_once 'steamid.class.php';
                    $input = $_POST["profile_url"];
                    $api_key = "XXXXXXXXXXXXXXXXXXXX";
                    $id = new SteamID($input,$api_key);

                    if ($id->resolveVanity()) {
                        $avatar = $id->toAvatar();
                        echo $avatar;
                    }

               ?>">

            </body>


    <?php

    require_once 'steamid.class.php';


    function urlCheck() {
        $input = $_POST["profile_url"];
        if(substr_count($input, ' ') === strlen($input)) {
          echo "Enter URL";

          return false;
        } else {
          return true;
        }

    }

    if(isset($_POST['submit']))
    {
       display();
    }

    function display() {

        require_once 'steamid.class.php';
        $input = $_POST["profile_url"];
        $api_key = "XXXXXXXXXXXX";
        $id = new SteamID($input,$api_key);

        if(urlCheck()) {

            if ($id->resolveVanity()) {



            $communityid = $id->toCommunityID();
            echo $communityid . ", " . " ";

            $steamid = $id->toSteamID();
            echo $steamid . ", " . " ";

            global $userid;

            $userid = '[U:1:'.$id->toUserID().']';
            echo $userid . ", " . " ";



            } else {
                echo "Profile wasnt found!";
            }

        }
    }


    ?>

steamid.class.php

<?php

        class SteamID {

          protected $id;
          protected $key;

          public function __construct($id,$api_key = '') {
            $this->id = $id;
            $this->key = $api_key;
            return $this;
          }

          public function isID32() {
            if(preg_match('/^STEAM_0:[01]:[0-9]{8,9}$/', $this->id)) {
                return true;
            }
            return false;
          }

          public function isID64() {
            if(preg_match('/7656119[0-9]{10}/i', $this->id)) {
              $this->id = $this->cleanOutput(str_replace('https://steamcommunity.com/profiles/', '', $this->id));
              return true;
            }
            return false;
          }

          public function resolveVanity() {
            $search = $this->cleanOutput(str_replace('https://steamcommunity.com/id/', '', $this->id));
            $api = 'http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key='.$this->key.'&vanityurl='.$search;
            $vanity = $this->getCURL($api);
            if ($vanity['response']['success'] === 1) {
              $this->id = $vanity['response']['steamid'];
              return true;
            }
            else {
              return false;
            }
          }

          public function toAvatar() {
            if ($this->isID32() || $this->isID64() || $this->resolveVanity()) {
              $key = $this->toCommunityID();
              $api = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='.$this->key.'&steamids='.$key;
              $data = $this->getCURL($api);
              $image = $data['response']['players'][0]['avatarfull'];
              return $image;
            }
            else {
              return false;
            }
          }

          public function toCommunityID() {
              if (preg_match('/^STEAM_/', $this->id)) {
                  $parts = explode(':', $this->id);
                  return bcadd(bcadd(bcmul($parts[2], '2'), '76561197960265728'), $parts[1]);
              } elseif (is_numeric($this->id) && strlen($this->id) < 16) {
                  return bcadd($this->id, '76561197960265728');
              } else {
                  return $this->id;
              }
          }

          public function toSteamID() {
              if (is_numeric($this->id) && strlen($this->id) >= 16) {
                        $this->id = bcsub($this->id, '76561197960265728');
                        //If subtraction goes negative, shift value up
                        if ($this->id < 0) {
                            $this->id += 4294967296;
                        }
                  $z = bcdiv($this->id, '2');
              } elseif (is_numeric($this->id)) {
                  $z = bcdiv($this->id, '2');
              } else {
                  return $this->id;
              }
              $y = bcmod($this->id, '2');
              return 'STEAM_0:' . $y . ':' . floor($z);
          }

          public function toUserID() {
              if (preg_match('/^STEAM_/', $this->id)) {
                  $split = explode(':', $this->id);
                  echo $split;
                  return $split[2] * 2 + $split[1];
              } elseif (preg_match('/^765/', $this->id) && strlen($this->id) > 15) {

                        $uid = bcsub($this->id, '76561197960265728');
                        if ($uid < 0) {

                            $uid += 4294967296;
                        }
                        return  $uid;

              } else {
                  return $this->id;

              }
          }

          //Function to sent request to SteamAPI
          private function getCURL($url) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
            curl_setopt($ch, CURLOPT_TIMEOUT, 2);
            $request = curl_exec($ch);
            curl_close($ch);
            $json = json_decode($request, true);
            return $json;
          }

          //Remove end slash if present as well as trailing whitespace
          private function cleanOutput($input) {
            $specialInput = htmlspecialchars(str_replace('/', '', $input));
            $cleanInput = preg_replace('/\s/', '', $specialInput);
            return $cleanInput;
          }

        }

问候:)

我有一个带有“提交”按钮的表单,单击该表单将调用display()函数。 display()是一个函数,其中包括另一个.php文件,在该文件中,我可以使用该函数对......>

php html variables echo
1个回答
0
投票

正确的方法是从函数中返回对象并根据需要使用它。请注意,我正在使用PHP速记回显(<?=)。更好的方法是使用另一个函数来使用期望的字符串格式化变量,因此您无需在HTML中进行格式化。

function urlCheck() {
    $input = $_POST["profile_url"];
    if(substr_count($input, ' ') === strlen($input)) {
      echo "Enter URL";

      return false;
    } else {
      return $input;
    }
}

function display() {
    $input = urlCheck();
    if($input) {
      require_once 'steamid.class.php';
      $api_key = "XXXXXXXXXXXX";
      $id = new SteamID($input,$api_key);
        if ($id->resolveVanity()) {
            return $id;
        } else {
            echo "Profile wasn't found!";

            return false;
        }
    }
}

if(isset($_POST['submit']))
{
  $id = display();
}
...
<h1><?= ($id) ? "[U:1:".$id->toUserID()."], " . " " : "" ?></h1>
...
<img onerror="this.style.display='none'" src="<?= ($id) ? {$id->toAvatar()} : "" ?>">
© www.soinside.com 2019 - 2024. All rights reserved.