You want to change spaces to tabs (or tabs to spaces) in a string while keeping text aligned with tab stops. For example, you want to display formatted text to users in a standardized way.
Use str_replace( )
to switch spaces to tabs or tabs to
spaces:
$r = mysql_query("SELECT message FROM messages WHERE id = 1") or die(); $ob = mysql_fetch_object($r); $tabbed = str_replace(' ',"\t",$ob->message); $spaced = str_replace("\t",' ',$ob->message); print "With Tabs: <pre>$tabbed</pre>"; print "With Spaces: <pre>$spaced</pre>";
Using str_replace( )
for conversion, however,
doesn’t respect tab stops. If you want tab stops
every eight characters, a line beginning with a five-letter word and
a tab should have that tab replaced with three spaces, not one. Use
the pc_tab_expand( )
function shown in Example 1-1 to turn tabs to spaces in a way that respects
tab stops.
Example 1-1. pc_tab_expand( )
function pc_tab_expand($a) { $tab_stop = 8; while (strstr($a,"\t")) { $a = preg_replace('/^([^\t]*)(\t+)/e', "'\\1'.str_repeat(' ',strlen('\\2') * $tab_stop - strlen('\\1') % $tab_stop)",$a); } return $a; } $spaced = pc_tab_expand($ob->message);
You can use the
pc_tab_unexpand()
function shown in Example 1-2 to turn spaces back to tabs.
Example 1-2. pc_tab_unexpand( )
function pc_tab_unexpand($x) { $tab_stop = 8; $lines = explode("\n",$x); for ($i = 0, $j = count($lines); $i < $j; $i++) { $lines[$i] = pc_tab_expand($lines[$i]); $e = preg_split("/(.\{$tab_stop})/",$lines[$i],-1,PREG_SPLIT_DELIM_CAPTURE); $lastbit = array_pop($e); if (!isset($lastbit)) { $lastbit = ''; } if ($lastbit == str_repeat(' ',$tab_stop)) { $lastbit = "\t"; } for ($m = 0, $n = count($e); $m < $n; $m++) { $e[$m] = preg_replace('/ +$',"\t",$e[$m]); } $lines[$i] = join('',$e).$lastbit; } $x = join("\n", $lines); return $x; } $tabbed = pc_tab_unexpand($ob->message);
Both functions take a string as an argument and return the string appropriately modified.
Each function assumes tab stops are every eight spaces, but that can
be modified by changing the setting of the
$tab_stop
variable.
The regular expression in pc_tab_expand( )
matches
both a group of tabs and all the text in a line before that group of
tabs. It needs to match the text before the tabs because the length
of that text affects how many spaces the tabs should be replaced so
that subsequent text is aligned with the next tab stop. The function
doesn’t just replace each tab with eight spaces; it
adjusts text after tabs to line up with tab stops.
Similarly, pc_tab_unexpand( )
doesn’t just look for eight consecutive spaces and
then replace them with one tab character. It divides up each line
into eight-character chunks and then substitutes ending whitespace in
those chunks (at least two spaces) with tabs. This not only preserves
text alignment with tab stops; it also saves space in the string.
Documentation on str_replace( )
at
http://www.php.net/str-replace.
Get PHP Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.