【问题标题】:How do I view php source code in php?如何在php中查看php源代码?
【发布时间】:2014-09-09 06:06:57
【问题描述】:

我想用php输出一些php代码,但我似乎找不到正确的格式。

我查看代码的功能是这样的:

public function openFile($path, $save = false)
    {
    if($save == true) $filename = basename($path); else $filename = "tmp/".substr(hash("sha256", time()), 8, 25).".tmp";//Datei speichern
    #$fp = fopen($filename, "wr");
    $url = "ftp://".$this->username.":".$this->password."@".$this->host.":".$this->port.$path;
    $handle = curl_init();
    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($handle, CURLOPT_UPLOAD, false);
    curl_setopt($handle, CURLOPT_HEADER, true);
    #curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: text/plain;"));
    #curl_setopt($handle, CURLOPT_FILE, $fp);
    $result = curl_exec($handle);
    $info = curl_getinfo ($handle);
    curl_close($handle);
    header("Content-Type: text/plain;");
    echo htmlentities($result);
    return [$result, $info];
    }

我有一个“ajaxActions.php”,用于处理所有操作:

<?php
require_once('../classes/ftp.class.php');
$webftp = new FTP("127.0.0.1", 21, "root", "123", false, true);


if(!empty($_REQUEST["action"]) && !empty($_REQUEST["path"]))
    {
    switch ($_REQUEST["action"])
        {
        case "getDirs":
            $dirs = $webftp->listDirectory($_REQUEST["path"]);
            echo "<tr data-type=\"dir\">";
            echo "<td></td>";
            echo "<td><a onClick=\"dirUp();\" href=\"#\">..</a></td>";
            echo "<td>".$webftp->getSize("..")."</td>";
            echo "<td></td>";
            echo "<td></td>";
            echo "</tr>";
            foreach ($dirs as $dir)
                {
                // Überprüfen ob der Pfad ein Verzeichnis ist, falls ja, kann man es betreten ansonsten wird die Datei angezeigt
                if($webftp->ftpIsDir($dir)==true) $data = "folder-close"; else $data = "file";

                $aTag = "<a onClick=\"reloadTable('".$dir."');\" href=\"#\">".$dir."</a>";
                if($data == "file") $aTag = "<a onClick=\"openFile('".$dir."');\" href=\"#\">".$dir."</a>";
                echo "<tr data-type=\"".$data."\">";
                echo "<td><i class=\"glyphicon glyphicon-".$data."\"></i></td>";
                echo "<td>".$aTag."</td>";
                echo "<td>".$webftp->getSize($dir)."</td>";
                echo "<td>".($data == "file" ? '<i class="glyphicon glyphicon-eye-open"></i>' : '')."<i class=\"glyphicon glyphicon-pencil\"></i><i class=\"glyphicon glyphicon-floppy-save\"></i><i class=\"glyphicon glyphicon-remove\"></i></td>";
                echo "<td></td>";
                echo "</tr>";
                }
            break;

        case "dirUp":
            $webftp->dirUp();
            break;

        case "openFile":
            $webftp->openFile($_REQUEST["path"]);
            break;

        default:
            # code...
            break;
        }
    }
?>

在我的 index.php 上,我通过 jQuery 解析代码:

function openFile(path) {
  $.get("lib/includes/ajaxActions.php?action=openFile&path=" + path, function(data) {
    console.log(data);
    $('#editor').html(data);
    $('#sourceCode').modal('show')
  });
}

console.log() 返回正确的代码,但在 div#editor 上没有语法,所有的中断都消失了。

输出: console.log():

&lt;?php

    class FTP  {
    private $host = &quot;&quot;;
    private $port = &quot;&quot;;
    private $username = &quot;&quot;;
    private $password = &quot;&quot;;
    private $passive = false;
    private $connection = false;
    private $ssl = false;
    private $dir = false;


    public function __construct($host, $port = 21, $username, $password, $ssl = false, $passive = false)
        {
        if ( !extension_loaded('ftp') )
            {
            throw new Exception('FTP extension is not loaded!');
            }
        $this-&gt;host = $host;
        $this-&gt;port = $port;
        $this-&gt;username = $username;
        $this-&gt;password = $password;
        $this-&gt;ssl = $ssl;
        $this-&gt;passive = $passive;

        $this-&gt;connect($this-&gt;host, $this-&gt;port, $this-&gt;ssl, 90);
        $this-&gt;login($this-&gt;username, $this-&gt;password);
        }

div#editor:

<?php class FTP { private $host = ""; private $port = ""; private $username = ""; private $password = ""; private $passive = false; private $connection = false; private $ssl = false; private $dir = false; public function __construct($host, $port = 21, $username, $password, $ssl = false, $passive = false) { if ( !extension_loaded('ftp') ) { throw new Exception('FTP extension is not loaded!'); } $this->host = $host; $this->port = $port; $this->username = $username; $this->password = $password; $this->ssl = $ssl; $this->passive = $passive; $this->connect($this->host, $this->port, $this->ssl, 90); $this->login($this->username, $this->password); } public function connect($host ,$port = 21, $ssl = false, $timeout = 90) { if ($ssl) { $this->connection = ftp_ssl_connect($host, $port, $timeout); } else { $this->connection = ftp_connect($host, $port, $timeout); } if ($this->connection == null) { throw new Exception('Unable to connect'); } else { return $this; } }

【问题讨论】:

标签: php jquery viewer


【解决方案1】:

变化:

echo htmlentities($result);

到:

echo '<code>' . nl2br(htmlentities($result)) . '</code>';

nl2br 在所有换行符之前添加 &lt;br&gt; 标记。添加&lt;code&gt; 应该保持代码的格式。虽然我不确定您是否可以依赖 Tab 正确排列——浏览器的制表位可能与您的 IDE 不同。

【讨论】:

  • 但是标签是什么?所有选项卡都呈现为一个空字符
  • 我不知道ace是什么。
  • 看看这个:ace.c9.io/#nav=about 我发现了问题所在。我无法使用 $('#editor').html(data); 设置文本
【解决方案2】:

您要渲染的输出字符串必须正确转义。这意味着用他们的 html corespondents 替换你所有的“”以及可能的其他字符。 长话短说,不要多次使用 echo,而是将所有文本分配给 $var 并执行 echo htmlentities($var);。对于 PHP 标签,我认为您必须手动将“”替换为它们的 urlEncoded 值。 我希望它至少能有所帮助。

【讨论】:

  • 浏览器呈现“<”到“
  • 他正在使用echo htmlentities($result) 来完成这项工作。
【解决方案3】:

我找到了解决方案: 我的 javascript 代码如下所示:

var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/php");
editor.setOptions({
    maxLines: 50
});

function openFile(path) {
    $.get("lib/includes/ajaxActions.php?action=openFile&path=" + path, function(data) {
        editor.setValue(data);
        $('#sourceCode').modal('show')
    });
}

我的phpcode:

public function openFile($path, $save = false)
        {
        if($save == true) $filename = basename($path); else $filename = "tmp/".substr(hash("sha256", time()), 8, 25).".tmp";//Datei speichern
        #$fp = fopen($filename, "wr");
        $url = "ftp://".$this->username.":".$this->password."@".$this->host.":".$this->port.$path;
        $handle = curl_init();
        curl_setopt($handle, CURLOPT_URL, $url);
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($handle, CURLOPT_UPLOAD, false);
        curl_setopt($handle, CURLOPT_HEADER, true);
        #curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: text/plain;"));
        #curl_setopt($handle, CURLOPT_FILE, $fp);
        $result = curl_exec($handle);
        $info = curl_getinfo ($handle);
        curl_close($handle);
        #header("Content-Type: text/plain;");
        echo $result;
        return [$result, $info];
        }

【讨论】:

    猜你喜欢
    • 2016-01-01
    • 2014-10-24
    • 2018-10-02
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    相关资源
    最近更新 更多