在某些 php 的输出中添加一个 id

我有一个小代码,


我需要在输出中添加一个类


$output_html .= ' ' . groovy_menu_woocommerce_mini_cart_counter( $qty ) . ' ';

输出一个


<span class="gm-cart-counter">2</span>

如何在输出中添加一个id


' . groovy_menu_woocommerce_mini_cart_counter( $qty ) . '

所以它看起来像这样


<span id="newid"class="gm-cart-counter">2</span>

新代码测试


$span = groovy_menu_woocommerce_mini_cart_counter($qty);

$spanWithId = "<span id='the-id'" ;

该工作并显示带有 ID id 的跨度


但这并没有


$span = groovy_menu_woocommerce_mini_cart_counter($qty);

$spanWithId = "<span id='the-id'"  . $span;


拉莫斯之舞
浏览 143回答 2
2回答

蛊毒传说

这很丑陋,但它应该可以解决问题:$span = groovy_menu_woocommerce_mini_cart_counter($qty);$spanWithId = "<span id='the-id'" . substr($span, 5, strlen($span));$output_html .= $spanWithId;echo "$spanWithId";输出:<span id='the-id' class="gm-cart-counter">2</span>尝试这个&nbsp; &nbsp; global $woocommerce;&nbsp; &nbsp; $qty = 0;&nbsp; &nbsp; if ($tks == true) {&nbsp; &nbsp; &nbsp; &nbsp; $qty = $woocommerce->cart->get_cart_contents_count();&nbsp; &nbsp; }&nbsp; &nbsp; $cartIcon = 'fa fa-shopping-cart';&nbsp; &nbsp; $span = groovy_menu_woocommerce_mini_cart_counter($qty);&nbsp; &nbsp; $spanWithId = "<span id='the-id'" . substr($span, 5, strlen($span));&nbsp; &nbsp; $output_html .= '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="gm-minicart minicartmarie">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="' . get_permalink( wc_get_page_id( 'cart' ) ) . '" class="gm-minicart-link minicartmarie">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="gm-badge">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <i class="gm-icon ' . esc_attr( $cartIcon ) . '"></i>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ' . $spanWithId . '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ';您正确地替换了对的调用,groovy_menu_woocommerce_mini_cart_counter($qty)但;在中断它的语句中包含了 a 。此外,而不是echoing$spanWithId你应该echo $output_html

慕码人2483693

这就是我要做的。// create a wrapper function that injects the idfunction menu_with_id($qty, $id){&nbsp; $span = groovy_menu_woocommerce_mini_cart_counter($qty);&nbsp; return str_replace('<span', "<span id='$id'", $span);}// then replace it$output_html .= ' ' . menu_with_id($qty, $id) . ' ';
打开App,查看更多内容
随时随地看视频慕课网APP