在 php 模板的页面上将 PHP/HTML 显示为文本

Show PHP/HTML as text on page in php template

如何在 'pre' 和 'code' 标签内的页面上吐出以下代码作为文本,而不用我的 php 模板实际尝试读取 php代码。使用 js 和 css,'code' 和 'pre' 标签停止来自 运行 的实际代码。我如何使用 HTML/PHP.

的以下组合来执行此操作

我试过使用 htmlescaping,但这只会吐出 php。我不知道这里的正确组合是什么。

<pre>
                <code>
                <?php elseif(get_row_layout() == 'three_col_panel'): ?>

                <!-- ==================================== -->
                <!-- === 3 COLUMN ICON TEXT GRID ======== -->
                <!-- ==================================== -->

                <div class="icon-panel-three block">
                    <div class="main-wrap">
                        <div class="container">
                            <div class="row">
                                <?php if( have_rows('pillar')): ?>
                                    <?php while ( have_rows('pillar')) : the_row() ?>
                                        <div class="col-md-4 icon-column">
                                            <div class="inner-content">
                                                <?php $image = get_sub_field('image'); 
                                                            $url = $image['url'];
                                                            $alt = $image['alt'];
                                                            $size = 'medium';
                                                            $thumb = $image['sizes'][$size];
                                                    ?>
                                                <a href="<?= get_sub_field('image_link'); ?>"><img src="<?= $thumb; ?>" alt="<?= $alt; ?>"/></a>
                                                <div class="text">
                                                    <h1><a href="<?= get_sub_field('image_link'); ?>"><?= get_sub_field('heading')?></a></h1>
                                                    <p><?= get_sub_field('paragraph')?></p>
                                                </div><!--end text-->
                                                <?php if( have_rows('column_link')): ?>
                                                    <?php while ( have_rows('column_link')) : the_row() ?>
                                                        <a href="<?= get_sub_field('col_link_url'); ?>" class="body-button"><?= get_sub_field('col_link_text'); ?><img src="<?= get_template_directory_uri(); ?>/img/button-arrow.svg" alt="click here" /></a>
                                                    <?php endwhile; ?>
                                                <?php endif; ?>
                                            </div><!--end inner-content-->
                                        </div><!--end col-->
                                    <?php endwhile; ?>
                                <?php endif; ?>
                            </div><!--end row-->
                        </div><!--end container-->
                    </div><!--end main-wrap-->
                </div><!--end tile-panel-->

            </code>
            </pre>

你只需要将所有内容放在双引号之间:“...”,并转义里面的双引号:

<div class=\"icon-panel-three block\">
<div class=\"main-wrap\">
.... etc

前提条件:

要输出的文本必须在可变字符串中或在单独的文件中。

我建议你文件解决方案:更简单,代码更清晰,一个文件比一个代码显示更多。

如果您更喜欢字符串方式,则必须用单引号将其括起来并转义内部单引号:

$string = '<?php elseif(get_row_layout() == \'three_col_panel\'): ?>

                <!-- ==================================== -->
           (...)';

您不能使用双引号,因为它们允许 php 变量计算。

如果你愿意,你也可以使用Nowdoc string syntax:在这种情况下,不需要转义。

最佳方式:

如果要输出完整的文件,请使用highlight_string( $string ) if your text is a string, or highlight_file( $filepath )

你不需要用<code></code>包装它:上面的命令为你包装它。

作为礼物,您将获得彩色语法!

phpFiddle demo

替代方法:

对于字符串:

<code><?php echo htmlentities( $string ); ?></code>

对于文件:

<code><?php echo htmlentities( file_get_contents( $filepath ) ); ?></code>

您可以做的另一件事是:将其全部放入一个 .txt 文件中并使用 php <?php $homepage = file_get_contents('mycode.txt'); echo $homepage; ?>

显示内容

使用 php 您可以像这样尝试 php 函数“highlight_string();”:

<?php
highlight_string('<?php phpinfo(); ?>');
?>

它将 ('input') 输出为带有颜色编码的字符串。 查看其 php 文档:http://php.net/manual/en/function.highlight-string.php