我正在创建一些自定义的 Woocommerce 我的帐户页面。一切都很顺利,但我遇到了一小段我无法通过的麻烦代码。
我的目标是简单地在 $address = apply_filters( 'woocommerce_my_account_my_address_formatted_address', ...
的输出之间添加间距
目前,它填充 <br>
(甚至不是 <br />
)字段值之间的标签。我真的希望能够替换 <br>
带有 <p>
的标签标签。就是这样。如果我能理解如何在它们之间添加我想要的任何内容,那就太好了,但这并不重要。
所以,这里是部分:
<address>
<?php
$address = apply_filters( 'woocommerce_my_account_my_address_formatted_address', array(
'first_name' => get_user_meta( $customer_id, $name . '_first_name', true ),
'last_name' => get_user_meta( $customer_id, $name . '_last_name', true ),
'company' => get_user_meta( $customer_id, $name . '_company', true ),
'address_1' => get_user_meta( $customer_id, $name . '_address_1', true ),
'address_2' => get_user_meta( $customer_id, $name . '_address_2', true ),
'city' => get_user_meta( $customer_id, $name . '_city', true ),
'state' => get_user_meta( $customer_id, $name . '_state', true ),
'postcode' => get_user_meta( $customer_id, $name . '_postcode', true ),
'country' => get_user_meta( $customer_id, $name . '_country', true )
), $customer_id, $name );
$formatted_address = WC()->countries->get_formatted_address( $address );
if ( ! $formatted_address )
_e( 'You have not set up this type of address yet.', 'woocommerce' );
else
echo $formatted_address;
?>
</address>
然后输出如下:
<address>
John Doe<br>John's Company<br>35 John Street<br>Johnsville, JD 55555
</address>
有人陪我吗?我当然是 PHP 新手 - 所以如果这真的很简单,请耐心等待。我只是在任何地方都没有找到解决方案。通常,我可以轻而易举地解决这类问题。
我知道这有点老了,但我遇到了完全相同的问题,并设法通过使用 preg_match() 搜索的正则表达式来解决它。
我所做的就是更改 $pattern
变量并包装 $output
在<p></p>
标签。最终代码为:
$address = apply_filters( 'woocommerce_my_account_my_address_formatted_address', array(
'first_name' => get_user_meta( $customer_id, $name . '_first_name', true ),
'last_name' => get_user_meta( $customer_id, $name . '_last_name', true ),
'company' => get_user_meta( $customer_id, $name . '_company', true ),
'address_1' => get_user_meta( $customer_id, $name . '_address_1', true ),
'address_2' => get_user_meta( $customer_id, $name . '_address_2', true ),
'city' => get_user_meta( $customer_id, $name . '_city', true ),
'state' => get_user_meta( $customer_id, $name . '_state', true ),
'postcode' => get_user_meta( $customer_id, $name . '_postcode', true ),
'country' => get_user_meta( $customer_id, $name . '_country', true )
), $customer_id, $name );
$formatted_address = WC()->countries->get_formatted_address( $address );
if ( ! $formatted_address )
_e( 'You have not set up this type of address yet.', 'woocommerce' );
else
$pattern = "/<br\/>/";
$replacements= '<p>';
$formatted_address = WC()->countries->get_formatted_address( $address );
$output = preg_replace($pattern, $replacements, $formatted_address);
echo "<p>" . $output . "</p>";
我是一名优秀的程序员,十分优秀!