从 PHP 回显 HTML 后删除新行
Remove new lines after echoing HTML from PHP
我有一个 Wordpress 功能,可以检查用户是否填写了他或她的个人资料中的某些字段。如果已填写,则回显其内容,如果没有:不要。这是有趣的精简版
<ul>
<?php if (get_the_author_meta('url',$theID)) : ?>
<li class="url">
<a href="<?php the_author_meta('url',$theID); ?>" title="">Website</a>
</li>
<?php endif; // End check for url ?>
<?php if ( get_the_author_meta('twitter',$theID)) : ?>
<li class="twitter">
<a href="http://twitter.com/<?php the_author_meta('twitter',$theID); ?>" title="">Twitter</a>
</li>
<?php endif; // End check for twitter ?>
<?php if ( get_the_author_meta('instagram',$theID)) : ?>
<li class="instagram">
<a href="http://instagram.com/<?php the_author_meta('instagram',$theID); ?>" title="">Instagram</a>
</li>
<?php endif; // End check for instagram ?>
<?php if ( get_the_author_meta('linkedin',$theID)) : ?>
<li class="linkedin">
<a href="http://www.linkedin.com/<?php the_author_meta('linkedin',$theID); ?>" title="">LinkedIn</a>
</li>
<?php endif; // End check for linkedin ?>
</ul>
这很好用。但是,出于布局目的,我在这些元素上使用 inline
并且我不能使用浮动。您可能知道,这会导致元素之间出现 "gaps",因为它们都在新行上回显。
我不想要这些差距。我知道我可以通过任意负边距解决它,但我不想使用它,因为我使用的是响应式、基于百分比的布局。解决方案是 HTML 中的 "fill the gaps" 通过将所有 HTML 粘合在一起而不换行。例如:
内联显示时会产生间隙:
<ul>
<li>Twitter</li>
<li>Instagram</li>
<li>LinkedIn</li>
</ul>
不会有间隙:
<ul>
<li>Twitter</li><li>Instagram</li><li>LinkedIn</li>
</ul>
我正在寻找一个 PHP 解决方案(没有 CSS),它可以去掉 ul
中的所有新行,但我不知道从哪里开始.或者,更准确地说,我不知道将删除新行的函数放在哪里。我如何包装所有 HTML,然后将新行替换为... nothing?
你可以这样做:
ob_start();
//echo whatever stuff you need
echo str_replace(PHP_EOL, NULL, ob_get_clean());
使用 PHP_EOL
而不是 \n
或 \r\n
将捕获换行符,而不管平台如何。
你有没有尝试过类似的东西:
$string = str_replace("\n", "", $string);
或:
$string = str_replace("\n\r", "", $string);
(其中 $string 是您想要联机的 html)
您可以为此使用缓冲区:
<?php
function remove_white_spaces($buffer){
return trim(preg_replace('/\s\s+/', ' ', $buffer));
}
ob_start('remove_white_spaces');
?>
<ul>
<?php if (get_the_author_meta('url',$theID)) : ?>
<li class="url">
<a href="<?php the_author_meta('url',$theID); ?>" title="">Website</a>
</li>
<?php endif; // End check for url ?>
<?php if ( get_the_author_meta('twitter',$theID)) : ?>
<li class="twitter">
<a href="http://twitter.com/<?php the_author_meta('twitter',$theID); ?>" title="">Twitter</a>
</li>
<?php endif; // End check for twitter ?>
<?php if ( get_the_author_meta('instagram',$theID)) : ?>
<li class="instagram">
<a href="http://instagram.com/<?php the_author_meta('instagram',$theID); ?>" title="">Instagram</a>
</li>
<?php endif; // End check for instagram ?>
<?php if ( get_the_author_meta('linkedin',$theID)) : ?>
<li class="linkedin">
<a href="http://www.linkedin.com/<?php the_author_meta('linkedin',$theID); ?>" title="">LinkedIn</a>
</li>
<?php endif; // End check for linkedin ?>
</ul>
<?php ob_end_flush(); ?>
捕获缓冲区中的所有输出,运行一个用空格替换新行并释放缓冲区的函数。
只需将您的 ul 字体大小设置为 0 并将该大小应用于您的 li
font-size: 15px; display: inline-block;
根据@APAD1 的评论和@CBRoe 的反馈,我决定采用评论方法。尽管 HTML 输出不漂亮(空注释,再见语义),但它可以完美运行。
<ul class="author-meta">
<?php if (get_the_author_meta('url',$theID)) : ?><li class="url">
<a href="<?php the_author_meta('url',$theID); ?>" title="">Website</a>
</li><?php endif; // End check for url ?><!--
--><?php if ( get_the_author_meta('twitter',$theID)) : ?><li class="twitter">
<a href="http://twitter.com/<?php the_author_meta('twitter',$theID); ?>" title="">Twitter</a>
</li><?php endif; // End check for twitter ?><!--
--><?php if ( get_the_author_meta('instagram',$theID)) : ?><li class="instagram">
<a href="http://instagram.com/<?php the_author_meta('instagram',$theID); ?>" title="">Instagram</a>
</li><?php endif; // End check for instagram ?><!--
--><?php if ( get_the_author_meta('linkedin',$theID)) : ?><li class="linkedin">
<a href="http://www.linkedin.com/<?php the_author_meta('linkedin',$theID); ?>" title="">LinkedIn</a>
</li><?php endif; // End check for linkedin ?>
</ul>
请注意,注释应该在 if
子句之外,因为如果它们在内部,则可能无法正确回显,例如缺少开始或结束标记。
给你:
<ul><?php
$output = '';
if (get_the_author_meta('url', $theID)) {
$output .= '<li class="url"><a href="' . the_author_meta('url',$theID) . '" title="">Website</a></li>';
} elseif (get_the_author_meta('twitter', $theID)) {
$output .= '<li class="twitter"><a href="http://twitter.com/' . the_author_meta('twitter', $theID) . '" title="">Twitter</a></li>';
} elseif (get_the_author_meta('instagram', $theID)) {
$output .= '<li class="instagram"><a href="http://instagram.com/' . the_author_meta('instagram', $theID) . '" title="">Instagram</a></li>';
} elseif (get_the_author_meta('linkedin',$theID)) {
$output .= '<li class="linkedin"><a href="http://www.linkedin.com/' . the_author_meta('linkedin', $theID) . '" title="">LinkedIn</a></li>';
}
echo $output;
?></ul>
<ul class="author-meta">
<?php
if (get_the_author_meta('url', $theID)) {
$output = '<li class="url"><a href="' . get_the_author_meta('url', $theID) . '" title="">Website</a></li>';
}
if ( get_the_author_meta('twitter', $theID)) {
$output .= '<li class="twitter"><a href="http://twitter.com/' . get_the_author_meta('twitter', $theID) . '" title="">Twitter</a></li>';
}
if ( get_the_author_meta('instagram', $theID)) {
$output .= '<li class="instagram"><a href="http://instagram.com/' . get_the_author_meta('instagram', $theID) . '" title="">Instagram</a></li>';
}
if ( get_the_author_meta('linkedin', $theID)) {
$output .= '<li class="linkedin"><a href="http://www.linkedin.com/' . get_the_author_meta('linkedin', $theID) . '" title="">LinkedIn</a></li>';
}
echo $output;
?>
</ul>
Bram Vanroy 编辑:使用 PHP.
中的函数时,确保使用 get_the_author_meta
而不是 the_author_meta
要制作一个易于使用的版本,您可以将所有网站数据存储在一个数组中:
$site=Array(
"url"=>"Website",
"twitter"=> "Twitter",
"instagram"=> "Instagram",
"linkedin"=>"LinkedIn"
);
然后生成整个列表。我使用了一个简单的示例案例,其中所有域都是 .com。如果要存储更多信息,则必须改进数组格式。
<ul><?php
foreach($site as $k=>$v)
if (get_the_author_meta($k,$theID))
echo '<li class="'.$k.'"><a href="'
.(($k=="url")?"":"http://$k.com/")
.the_author_meta($k,$theID).'">'.$v.'</a></li>';
?></ul>
不要忘记删除 a
标签中的空标题属性。
请注意,还有其他 CSS 替代方案:在 ul
上使用 display:table
,在 li
上使用 display:table-cell
,或者使用 display: flex
等等。
我有一个 Wordpress 功能,可以检查用户是否填写了他或她的个人资料中的某些字段。如果已填写,则回显其内容,如果没有:不要。这是有趣的精简版
<ul>
<?php if (get_the_author_meta('url',$theID)) : ?>
<li class="url">
<a href="<?php the_author_meta('url',$theID); ?>" title="">Website</a>
</li>
<?php endif; // End check for url ?>
<?php if ( get_the_author_meta('twitter',$theID)) : ?>
<li class="twitter">
<a href="http://twitter.com/<?php the_author_meta('twitter',$theID); ?>" title="">Twitter</a>
</li>
<?php endif; // End check for twitter ?>
<?php if ( get_the_author_meta('instagram',$theID)) : ?>
<li class="instagram">
<a href="http://instagram.com/<?php the_author_meta('instagram',$theID); ?>" title="">Instagram</a>
</li>
<?php endif; // End check for instagram ?>
<?php if ( get_the_author_meta('linkedin',$theID)) : ?>
<li class="linkedin">
<a href="http://www.linkedin.com/<?php the_author_meta('linkedin',$theID); ?>" title="">LinkedIn</a>
</li>
<?php endif; // End check for linkedin ?>
</ul>
这很好用。但是,出于布局目的,我在这些元素上使用 inline
并且我不能使用浮动。您可能知道,这会导致元素之间出现 "gaps",因为它们都在新行上回显。
我不想要这些差距。我知道我可以通过任意负边距解决它,但我不想使用它,因为我使用的是响应式、基于百分比的布局。解决方案是 HTML 中的 "fill the gaps" 通过将所有 HTML 粘合在一起而不换行。例如:
内联显示时会产生间隙:
<ul>
<li>Twitter</li>
<li>Instagram</li>
<li>LinkedIn</li>
</ul>
不会有间隙:
<ul>
<li>Twitter</li><li>Instagram</li><li>LinkedIn</li>
</ul>
我正在寻找一个 PHP 解决方案(没有 CSS),它可以去掉 ul
中的所有新行,但我不知道从哪里开始.或者,更准确地说,我不知道将删除新行的函数放在哪里。我如何包装所有 HTML,然后将新行替换为... nothing?
你可以这样做:
ob_start();
//echo whatever stuff you need
echo str_replace(PHP_EOL, NULL, ob_get_clean());
使用 PHP_EOL
而不是 \n
或 \r\n
将捕获换行符,而不管平台如何。
你有没有尝试过类似的东西:
$string = str_replace("\n", "", $string);
或:
$string = str_replace("\n\r", "", $string);
(其中 $string 是您想要联机的 html)
您可以为此使用缓冲区:
<?php
function remove_white_spaces($buffer){
return trim(preg_replace('/\s\s+/', ' ', $buffer));
}
ob_start('remove_white_spaces');
?>
<ul>
<?php if (get_the_author_meta('url',$theID)) : ?>
<li class="url">
<a href="<?php the_author_meta('url',$theID); ?>" title="">Website</a>
</li>
<?php endif; // End check for url ?>
<?php if ( get_the_author_meta('twitter',$theID)) : ?>
<li class="twitter">
<a href="http://twitter.com/<?php the_author_meta('twitter',$theID); ?>" title="">Twitter</a>
</li>
<?php endif; // End check for twitter ?>
<?php if ( get_the_author_meta('instagram',$theID)) : ?>
<li class="instagram">
<a href="http://instagram.com/<?php the_author_meta('instagram',$theID); ?>" title="">Instagram</a>
</li>
<?php endif; // End check for instagram ?>
<?php if ( get_the_author_meta('linkedin',$theID)) : ?>
<li class="linkedin">
<a href="http://www.linkedin.com/<?php the_author_meta('linkedin',$theID); ?>" title="">LinkedIn</a>
</li>
<?php endif; // End check for linkedin ?>
</ul>
<?php ob_end_flush(); ?>
捕获缓冲区中的所有输出,运行一个用空格替换新行并释放缓冲区的函数。
只需将您的 ul 字体大小设置为 0 并将该大小应用于您的 li
font-size: 15px; display: inline-block;
根据@APAD1 的评论和@CBRoe 的反馈,我决定采用评论方法。尽管 HTML 输出不漂亮(空注释,再见语义),但它可以完美运行。
<ul class="author-meta">
<?php if (get_the_author_meta('url',$theID)) : ?><li class="url">
<a href="<?php the_author_meta('url',$theID); ?>" title="">Website</a>
</li><?php endif; // End check for url ?><!--
--><?php if ( get_the_author_meta('twitter',$theID)) : ?><li class="twitter">
<a href="http://twitter.com/<?php the_author_meta('twitter',$theID); ?>" title="">Twitter</a>
</li><?php endif; // End check for twitter ?><!--
--><?php if ( get_the_author_meta('instagram',$theID)) : ?><li class="instagram">
<a href="http://instagram.com/<?php the_author_meta('instagram',$theID); ?>" title="">Instagram</a>
</li><?php endif; // End check for instagram ?><!--
--><?php if ( get_the_author_meta('linkedin',$theID)) : ?><li class="linkedin">
<a href="http://www.linkedin.com/<?php the_author_meta('linkedin',$theID); ?>" title="">LinkedIn</a>
</li><?php endif; // End check for linkedin ?>
</ul>
请注意,注释应该在 if
子句之外,因为如果它们在内部,则可能无法正确回显,例如缺少开始或结束标记。
给你:
<ul><?php
$output = '';
if (get_the_author_meta('url', $theID)) {
$output .= '<li class="url"><a href="' . the_author_meta('url',$theID) . '" title="">Website</a></li>';
} elseif (get_the_author_meta('twitter', $theID)) {
$output .= '<li class="twitter"><a href="http://twitter.com/' . the_author_meta('twitter', $theID) . '" title="">Twitter</a></li>';
} elseif (get_the_author_meta('instagram', $theID)) {
$output .= '<li class="instagram"><a href="http://instagram.com/' . the_author_meta('instagram', $theID) . '" title="">Instagram</a></li>';
} elseif (get_the_author_meta('linkedin',$theID)) {
$output .= '<li class="linkedin"><a href="http://www.linkedin.com/' . the_author_meta('linkedin', $theID) . '" title="">LinkedIn</a></li>';
}
echo $output;
?></ul>
<ul class="author-meta">
<?php
if (get_the_author_meta('url', $theID)) {
$output = '<li class="url"><a href="' . get_the_author_meta('url', $theID) . '" title="">Website</a></li>';
}
if ( get_the_author_meta('twitter', $theID)) {
$output .= '<li class="twitter"><a href="http://twitter.com/' . get_the_author_meta('twitter', $theID) . '" title="">Twitter</a></li>';
}
if ( get_the_author_meta('instagram', $theID)) {
$output .= '<li class="instagram"><a href="http://instagram.com/' . get_the_author_meta('instagram', $theID) . '" title="">Instagram</a></li>';
}
if ( get_the_author_meta('linkedin', $theID)) {
$output .= '<li class="linkedin"><a href="http://www.linkedin.com/' . get_the_author_meta('linkedin', $theID) . '" title="">LinkedIn</a></li>';
}
echo $output;
?>
</ul>
Bram Vanroy 编辑:使用 PHP.
中的函数时,确保使用get_the_author_meta
而不是 the_author_meta
要制作一个易于使用的版本,您可以将所有网站数据存储在一个数组中:
$site=Array(
"url"=>"Website",
"twitter"=> "Twitter",
"instagram"=> "Instagram",
"linkedin"=>"LinkedIn"
);
然后生成整个列表。我使用了一个简单的示例案例,其中所有域都是 .com。如果要存储更多信息,则必须改进数组格式。
<ul><?php
foreach($site as $k=>$v)
if (get_the_author_meta($k,$theID))
echo '<li class="'.$k.'"><a href="'
.(($k=="url")?"":"http://$k.com/")
.the_author_meta($k,$theID).'">'.$v.'</a></li>';
?></ul>
不要忘记删除 a
标签中的空标题属性。
请注意,还有其他 CSS 替代方案:在 ul
上使用 display:table
,在 li
上使用 display:table-cell
,或者使用 display: flex
等等。