2017-03-07 75 views
0

以下代码:插入空格放入PHP数组变量

<?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> 
+0

如果他们”重新包装在HTML标签中,那么您可能需要通过DOM获取文本内容,因为任何解决方案剥离标签后都会破坏,以防有内部空间。 – apokryfos

+0

'get_the_term_list'的结果是什么? – m7913d

+0

@ m7913d请参阅最新的答案。 – egr103

回答

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; 
+0

前后属性为我做了诡计。感谢您的回答! – egr103

+0

欢迎:)高兴它帮助 – Soykot

0

快速和肮脏的,你可以做$terms = $terms.replace(/([A-Z])/g, ' $1').trim()。每首都和带尾随前添加空间/前导空格

+0

这似乎只是完全打破了页面。 – egr103

+0

你做错了,因为它不应该... – clearshot66