如何在没有我的 php 模板实际尝试读取 php 代码的情况下,在"pre"和"code"标签内的页面上将以下代码作为文本吐出。 使用 js 和 css,"代码"和"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>
前提条件:
要输出的文本必须位于变量字符串或单独的文件中。
我建议你文件解决方案:它更简单,代码更清晰,你可以用一个文件显示多个代码。
如果你更喜欢字符串方式,你必须用单引号括起来并转义内部单引号:
$string = '<?php elseif(get_row_layout() == ''three_col_panel''): ?>
<!-- ==================================== -->
(...)';
您不能使用双引号,因为它们允许 php 变量计算。
如果您愿意,也可以使用 Nowdoc 字符串语法:在这种情况下,不需要转义。
最佳方式:
如果文本是字符串,请使用highlight_string( $string )
,如果要输出完整的文件,请使用highlight_file( $filepath )
。
你不需要用<code></code>
来包装它:上面的命令为你包装它。
作为礼物,您将拥有彩色语法!
替代方式:
对于字符串:
<code><?php echo htmlentities( $string ); ?></code>
对于文件:
<code><?php echo htmlentities( file_get_contents( $filepath ) ); ?></code>
您只需要将所有这些放在双引号之间:"...",然后转义其中的双引号:
<div class='"icon-panel-three block'">
<div class='"main-wrap'">
.... etc
你可以
做的另一件事是:把它们全部放在一个.txt文件中,并用php显示内容<?php
$homepage = file_get_contents('mycode.txt');
echo $homepage;
?>
使用 php
你可以像这样尝试 php 函数 " highlight_string();
":
<?php
highlight_string('<?php phpinfo(); ?>');
?>
它将输出("输入")作为带有颜色编码的字符串。查看其PHP文档:http://php.net/manual/en/function.highlight-string.php