background image

?>

可以使用下例中的 pc_tab_ unexpand()

 

函数把空格符转换回制表符。

<?php

functionpc_tab_unexpand($text) {

$tab_stop= 8;

$lines=explode("\n",$text);

foreach($linesas$i=>$line) {

// Expand any tabs to spaces

$line= pc_tab_expand($line);

$chunks=str_split($line,$tab_stop);

$chunkCount=count($chunks);

// Scan all but the last chunk

for($j= 0;$j<$chunkCount- 1;$j++) {

$chunks[$j] = preg_replace('/ {2,}$/',"\t",$chunks[$j]);

}

// If the last chunk is a tab-stop's worth of spaces

// convert it to a tab; Otherwise, leave it alone

if($chunks[$chunkCount-1] ==str_repeat(' ',$tab_stop)) {

$chunks[$chunkCount-1] ="\t";

}

// Recombine the chunks

$lines[$i] = implode('',$chunks);

}

// Recombine the lines

returnimplode("\n",$lines);

}

$tabbed= pc_tab_unexpand($ob->message);

?>

 

这两个函数都以一个字符串作为参数,并返回经过修改后的结果字符串。

这两个函数都假定每 8 个字符设置一个制表位,不过这可以通过更改变量$tab_stops