停止 php 添加新行

发布于 2025-01-07 04:33:39 字数 729 浏览 2 评论 0原文

我正在尝试将我的 Word Press 博客中的帖子添加到 JavaScript 数组中,但是它不断添加新行并导致 JavaScript 中出现错误。这是代码

  <?php $num = 0 ; require($_SERVER['DOCUMENT_ROOT'] . '/blog/wp-load.php');query_posts('showposts=3');if (have_posts()) : while (have_posts()) : the_post(); ?>
    news_content[<?php echo $num; ?>]="<a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a>Posted on <?php the_time('l jS F, Y') ?><?php the_excerpt(); ?>";

  <?php $num = $num + 1; endwhile; else: echo "no posts"; endif; ?>
  <?php wp_reset_query(); ?>

the_excerpt(); 不断返回

post

和一个新行,导致错误

我怎样才能摆脱新行?

i am trying to add the posts from my word press blog to a javascript array however it keeps adding a new line and causing errors in the javascript. here is the code

  <?php $num = 0 ; require($_SERVER['DOCUMENT_ROOT'] . '/blog/wp-load.php');query_posts('showposts=3');if (have_posts()) : while (have_posts()) : the_post(); ?>
    news_content[<?php echo $num; ?>]="<a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a>Posted on <?php the_time('l jS F, Y') ?><?php the_excerpt(); ?>";

  <?php $num = $num + 1; endwhile; else: echo "no posts"; endif; ?>
  <?php wp_reset_query(); ?>

the_excerpt(); keeps returning <p>post</p> and a new line, causing a error

How can i get rid of the new line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请止步禁区 2025-01-14 04:33:40

最简单的方法是修改函数 the_excerpt

如果那不可能:

ob_start();
the_excerpt();
$out = ob_get_contents();
ob_end_clean();
echo rtrim($out);

The simplest would be to modify the function the_excerpt.

If that's not possible:

ob_start();
the_excerpt();
$out = ob_get_contents();
ob_end_clean();
echo rtrim($out);
他不在意 2025-01-14 04:33:40

如果它是 \n,您可以执行类似以下操作

str_replace("\n","",the_excerpt());

http://php.net/manual/en/function.str-replace.php
请记住,它可能是 \n\r\n,具体取决于您的系统。

我可以说“所有内容都在一行”php 真的不可读吗?它真的很难维护,更不用说为我们阅读/调试了。为什么不采用一些更似是而非的布局?

If it is a \n, you can just do something like

str_replace("\n","",the_excerpt());

see: http://php.net/manual/en/function.str-replace.php
Keeping in mind it might be \n or \r\n, depending on your system.

can I just remark that the "everything on one line" php is really unreadable? It's really hard to maintain, let alone to read/debug for us. Why not some more specious layout?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文