admin管理员组文章数量:1130349
I'm not sure why I can't get this to work. I'm trying to remove the that is added inside this shortcode...
[box] Text [/box]
Which results in this HTML output:
<div class="box"> Text </div>
I want to remove those spaces. I tried to usr str_replace, but it's not removing the   :
function infoButton($atts, $content = null) {
extract( shortcode_atts( array(
'class' => '',
), $atts ));
$str = '<div class="box ' . $class . '">' . do_shortcode($content) . '</div>';
$new_str = str_replace(' ','',$str);
return $new_str;
}
add_shortcode('box', 'infoButton');
I'm not sure why I can't get this to work. I'm trying to remove the that is added inside this shortcode...
[box] Text [/box]
Which results in this HTML output:
<div class="box"> Text </div>
I want to remove those spaces. I tried to usr str_replace, but it's not removing the   :
function infoButton($atts, $content = null) {
extract( shortcode_atts( array(
'class' => '',
), $atts ));
$str = '<div class="box ' . $class . '">' . do_shortcode($content) . '</div>';
$new_str = str_replace(' ','',$str);
return $new_str;
}
add_shortcode('box', 'infoButton');
Share
Improve this question
asked Mar 14, 2016 at 21:54
LBFLBF
5393 gold badges11 silver badges28 bronze badges
3
|
1 Answer
Reset to default 0This could be due to the do_shortcode running through wpautop, see here for details on disabling that:
https://stackoverflow/questions/5940854/disable-automatic-formatting-inside-wordpress-shortcodes
But as frogg3862 said, what you need to do instead of that is to just trim out the beginning and ending whitespace from $content to prevent the non-breaking space from being automatically added.
function infoButton($atts, $content = null) {
extract( shortcode_atts( array(
'class' => '',
), $atts ));
$str = '<div class="box ' . $class . '">' . do_shortcode( trim( $content ) ) . '</div>';
return $str;
}
add_shortcode('box', 'infoButton');
I'm not sure why I can't get this to work. I'm trying to remove the that is added inside this shortcode...
[box] Text [/box]
Which results in this HTML output:
<div class="box"> Text </div>
I want to remove those spaces. I tried to usr str_replace, but it's not removing the   :
function infoButton($atts, $content = null) {
extract( shortcode_atts( array(
'class' => '',
), $atts ));
$str = '<div class="box ' . $class . '">' . do_shortcode($content) . '</div>';
$new_str = str_replace(' ','',$str);
return $new_str;
}
add_shortcode('box', 'infoButton');
I'm not sure why I can't get this to work. I'm trying to remove the that is added inside this shortcode...
[box] Text [/box]
Which results in this HTML output:
<div class="box"> Text </div>
I want to remove those spaces. I tried to usr str_replace, but it's not removing the   :
function infoButton($atts, $content = null) {
extract( shortcode_atts( array(
'class' => '',
), $atts ));
$str = '<div class="box ' . $class . '">' . do_shortcode($content) . '</div>';
$new_str = str_replace(' ','',$str);
return $new_str;
}
add_shortcode('box', 'infoButton');
Share
Improve this question
asked Mar 14, 2016 at 21:54
LBFLBF
5393 gold badges11 silver badges28 bronze badges
3
-
1
Could you use PHP
trim()? – Howdy_McGee ♦ Commented Mar 14, 2016 at 21:58 -
I just tried
return trim($str," ")but no change. – LBF Commented Mar 14, 2016 at 22:03 -
1
You'd use
$str = '<div class="box ' . $class . '">' . do_shortcode( trim( $content ) ) . '</div>';– frogg3862 Commented Mar 14, 2016 at 23:04
1 Answer
Reset to default 0This could be due to the do_shortcode running through wpautop, see here for details on disabling that:
https://stackoverflow/questions/5940854/disable-automatic-formatting-inside-wordpress-shortcodes
But as frogg3862 said, what you need to do instead of that is to just trim out the beginning and ending whitespace from $content to prevent the non-breaking space from being automatically added.
function infoButton($atts, $content = null) {
extract( shortcode_atts( array(
'class' => '',
), $atts ));
$str = '<div class="box ' . $class . '">' . do_shortcode( trim( $content ) ) . '</div>';
return $str;
}
add_shortcode('box', 'infoButton');
本文标签: phpRemove ampnbsp from shortcode
版权声明:本文标题:php - Remove &nbsp; from shortcode 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749182324a2328814.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


trim()? – Howdy_McGee ♦ Commented Mar 14, 2016 at 21:58return trim($str," ")but no change. – LBF Commented Mar 14, 2016 at 22:03$str = '<div class="box ' . $class . '">' . do_shortcode( trim( $content ) ) . '</div>';– frogg3862 Commented Mar 14, 2016 at 23:04