将空格插入PHP数组变量中

时间:2017-03-07 16:01:39

标签: php wordpress

以下代码:

<?php $terms = get_the_term_list( $post->ID, 'projects' ); 
      $terms = strip_tags( $terms );?>
<?php echo $terms ?>

产生以下结果:

CateringCommercialHospitality

注意缺少空格。如何插入空格以使输出为:

Catering Commercial Hospitality

修改

get_the_term_list输出以下内容:

<a href="http://permalink" rel="tag">Commercial</a>

2 个答案:

答案 0 :(得分:0)

你可以$terms = $terms.replace(/([A-Z])/g, ' $1').trim()快速而肮脏。在每个大写之前添加空格并去掉尾随/前导空格

答案 1 :(得分:0)

一种方法是在属性之前使用:

<?php echo get_the_term_list( $post->ID, 'people', 'People: ', ', ' ); ?>

另一种方式,我用于我的项目:

function findarch_portfolio_categories() {

      $terms = get_the_terms( get_the_ID(), 'portfolio-category' );
      if(!empty($terms)){
      $portfolio_category_links = array();

      foreach ( $terms as $term ) {
          $portfolio_category_links[] = $term->name;
      }
      $on_portfolio_category = join( ", ", $portfolio_category_links );
      return sprintf( esc_html__( '%s', 'textdomain' ), esc_html( $on_portfolio_category ) );
    }
  }
  endif;