用str_repeat重复特殊字符

时间:2013-05-20 13:28:30

标签: php ascii special-characters repeat

我想重复特殊字符或TAB。

我尝试使用str_repeat(str,int)

//my function repeat
function tab($num){
    return str_repeat('	', $num);
}

//if I call
echo '<table>' . "\r\n";
echo tab(3) . '<tr>';

//the result
<table>
&#09;&#09;&#09<tr>;

我尝试过单引号和双引号的几种方法, 但结果总是错误的

3 个答案:

答案 0 :(得分:2)

使用\ t打印标签

function tab($num){
    return str_repeat('\t', $num);
}

答案 1 :(得分:1)

function tab($num){
    return str_repeat(html_entity_decode('&#09;'), $num);
}

答案 2 :(得分:0)

如果您想缩进网页的源代码,则应使用"\t"代替特殊字符。

function tab($num){
    return str_repeat('\t', $num);
}

否则,如果您需要在页面上缩进文字(用户会看到),请在标签后添加&nbsp;

<span>&#09;&#09;&#09;&nbsp;</span> some text

无论如何,你应该使用MVC而不是这些东西。