简体   繁体   English

删除代码空白(WordPress)

[英]Remove Code White-space (WordPress)

Basically I have a WordPress loop and I need all the code to be compressed and all unnecessary white-space to be stripped like so: 基本上,我有一个WordPress循环,我需要压缩所有代码,并剥离所有不必要的空格,如下所示:

<article><p>Content</p></article><article><p>Content</p></article><article><p>Content</p></article>

I am including the loop file using: 我使用以下方式包含循环文件:

<?php get_template_part('library/inc/loop'); ?>

Just wondering if there is a simple way like: 只是想知道是否有一个简单的方法,例如:

<?php trim( get_template_part('library/inc/loop') ); ?>

If this is possible all my problems would be solved, any help is much appreciated. 如果可以的话,我所有的问题都将得到解决,我们将不胜感激。 Cheers! 干杯!

To remove all leading and trailing whitespaces within HTML elements, you might use 要删除HTML元素中的所有前导和尾随空格,您可以使用

// Remove whitespace before '<'
$html = preg_replace('~\s+<~', '<', $html);
// Remove whitespace after '>'
$html = preg_replace('~>\s+~', '>', $html);

This will leave any whitespace within text intact. 这将使文本内的所有空格保持不变。

I'm not too familiar with the "back end" of WordPress, however you could try 我对WordPress的“后端”不太熟悉,但是您可以尝试

str_replace(" ","",$data);//Find whitespace " " and replace with ""

To clarify: whitespace when outputting, or whitespace in the source code? 澄清一下:输出时是空格还是源代码中的空格?

Or as you put try using trim(). 或者像您所说的那样尝试使用trim()。 I hope this helps you. 我希望这可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM