I have a script written in the crop.php file. I'm using it to perform some image processing. What I want is to get the code from this file and post it on the website, so that everyone will be able to see it.
6 回答
您可以使用highlight_file($file);
请注意评论,他们充满了关于解析行号等的建议。
对于第二种解决方案,文档还说明了以下内容。
“许多服务器被配置为自动突出显示带有 phps 扩展名的文件。例如,example.phps 在查看时将显示文件的语法突出显示源。要启用此功能,请将此行添加到 httpd.conf:AddType application/x- httpd-php-源 .phps"
又快又脏:
如果您只是删除<?php
应该执行此操作的顶部的,虽然它不会被很好地格式化,但如果用户查看源代码,它应该看起来像您看到的那样。
如果你想让它看起来不错,我假设有工具可以做到这一点。
您需要做的就是将 PHP 代码复制到 HTML 文档中。包裹在段落标签中,如下所示:<P> ... php code ... </P>
PHP 是一种服务器端语言。或者,您可以编写另一个 php 脚本,该脚本将从 file.php 中读取(通过 fopen() 函数)并将其作为文本发布。
这是一种方法:
<?php
ob_start();
?>
// code to be shown start
phpinfo();
echo "This echo statement isn't executed";
/* Provoke an error -- bogus SQL syntax */
$stmt = $dbh->prepare('bogus sql');
if (!$stmt) {
echo "\nPDO::errorInfo():\n";
print_r($dbh->errorInfo());
}
// code to be shown end
<?php
$code = ob_get_clean();
highlight_string($code);
?>
这将在您的网络浏览器中回显但不会执行:
// code to be shown start
phpinfo();
echo "This echo statement isn't executed";
/* Provoke an error -- bogus SQL syntax */
$stmt = $dbh->prepare('bogus sql');
if (!$stmt) {
echo "\nPDO::errorInfo():\n";
print_r($dbh->errorInfo());
}
// code to be shown end
但是,不要包含<?php
或?>
介于两者之间// code to be shown start
,// code to be shown end
否则它将执行。
另一种方法是:(将同时突出显示代码)
<?php
highlight_file("your_file_to_be_displayed.php");
?>
使用 PHP 的highlight_file()
函数
创建一个名为“source.php”的新文件并包含
show_source("crop.php");
或 2. 将文件的内容复制到一个名为“code.html”的新文件中。PHP 默认只处理 .php 文件。