如何使用 php 创建换行符

how to create line breaks using php

我不太擅长 PHP,但我正在尝试为某些 woocommerce 文本添加换行符。我试过:

add_filter('woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 10, 2 );
function woo_change_order_received_text( $str, $order ) {
echo nl2br(   $new_str = $str .  " 

You will shortly receive a confirmation email. We will email you again once your order has been dispatched.

With best wishes – and happy styling,

Wendy & Emma x
");
    return $new_str;
}

这给了我换行符,但也显示了第二次没有换行符的文本:

You will shortly receive a confirmation email. We will email you again once your order has been dispatched.

With best wishes – and happy styling,

Wendy & Emma x Thank you. Your order has been received. You will shortly receive a confirmation email. We will email you again once your order has been dispatched. With best wishes – and happy styling, Wendy & Emma x

如何让它只显示换行符一次的文本? 谢谢

您在设置值时使用 echo 并且只显示带换行符的值(使用 nl2br())。

第一次显示值的是echo。第二次是函数的 returned 值。

相反,您只需要 return 新值...

function woo_change_order_received_text( $str, $order ) {
    return nl2br( $str .  " 

You will shortly receive a confirmation email. We will email you again once your order has been dispatched.

With best wishes – and happy styling,

Wendy & Emma x
");
}