Subject: Getting tab characters to show up in output
Posted By: richard.york Post Date: 7/1/2003 9:19:20 PM
I was wondering if anyone had a solution to get '\t' tab characters to show up in copy pulled from a database.  

I've tried the following with no success:

$tab = "<span style=\"width: 10px\"></span>";
$body = str_replace('\t', $tab, $body);

& I also tried:

strtr ($body, '\t', $tab);

The <span> there doesn't show up in HTML source code.  So I'm uncertain if I am barking up the right tree.

Thanks in advance for suggestions!

: )
Rich


:::::::::::::::::::::::::::::::::
mailto:richy@smilingsouls.net
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::

Reply By: NotNowJohn Reply Date: 7/2/2003 4:13:15 AM
Your replace statement is OK, and you can see in View Source that TAB has replaced with span tag. However, the browser don't show the whitespaces in the manner they exist in the HTML source.
You can force the browser to show whitespaces using a <pre> tag. This statement force the browser to show TAB on the screen.
echo "<pre>word1\tword2</pre>";

Or, you can use several &nbsp; instead of span tag if you want to simulate TAB's displaying.

Regs,


...but the Soon is eclipsed by the Moon
Reply By: richard.york Reply Date: 7/2/2003 8:42:34 AM
>Your replace statement is OK, and you can see in View Source that >TAB has replaced with span tag. However, the browser don't show the >whitespaces in the manner they exist in the HTML source.
>You can force the browser to show whitespaces using a <pre> tag. >This statement force the browser to show TAB on the screen.

Well as I mentioned in my first post, the span tags weren't showing up in the HTML source.  I am aware of how HTML treats whitespace, and is irrelevent here in that CSS under a strict HTML 4.01 setting treats width attributes as it should.  

The <pre> tag just sucks all together, in that it breaks my template in some very ugly ways, instead of that I've used n12br() server side.  I'm pretty sure that the <pre> tag is either deprecated or a proprietary tag anyway.  The &nbsp; statement is also deprecated under HTML 4.01, that's why I was trying something with the span tag.  

So what I'm trying to get at is something that is HTML 4.01 compliant, something that can be used server side and something that only replaces the white space where there is supposed to be a '\t' tab character. If the replace statement I did is supposed to work maybe I should try it out on a few different submissions.  (I was only testing on one particular submission).

Thanks
: )
Rich


:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::

Reply By: nikolai Reply Date: 7/2/2003 2:20:00 PM
Usually, I use a combination of spaces and non-breaking spaces to simulate a tab-like visual effect.


<?php
$TAB = " &nbsp; &nbsp;";

echo "Hello,{$TAB}world.\n";

?>



Take care,

Nik
http://www.bigaction.org/
Reply By: natmaster Reply Date: 7/3/2003 2:26:23 PM
u sure u did all that before u echoed $body?

----------------------------
http://aeonofdarkness.com
Reply By: richard.york Reply Date: 7/5/2003 4:15:02 PM
>u sure u did all that before u echoed $body?

Yeah, this is what I've got going on with the entire $body variable, which typically correlates to a poem or short story submitted on my site.  I can see that tab characters exists in the data I'm testing on, but I have yet to be successful in getting the \t replacement to work.

Thanks for your suggestions on what to use for a tab, but all I really need is a successful replacement of the character.  I'm using the <span> tags to keep strict HTML 4.01/XHTML compliance.   Which again isn't really relevant in that so far as even that doesn't even show up in rendered HTML source code.  

$tab = "<span style=\"width: 10px\"></span>";
$body = stripslashes($body);
$body = str_replace('<BR>', ' ', $body);
$body = str_replace('<Br>', ' ', $body);
$body = str_replace('<bR>', ' ', $body);
$body = str_replace('<br>', ' ', $body);
$body = str_replace('&amp;quot;', '&quot;', $body);
$body = str_replace('\t', $tab, $body);

echo nl2br($body);

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::

Reply By: richard.york Reply Date: 8/11/2003 9:46:39 AM
*update*

For all interested parties, on a hunch I decided to change my string replacement function and used ereg_replace() instead of str_replace().  And it worked!

My HTML 4.01 compliant tab which also worked:
$tab = "<span style=\"margin-left: 10px\"></span>";

$submission_data["body"] = ereg_replace("\t", $tab, $submission_data["body"]);

Apparently when compared side by side, the ereg_replace() and str_replace() appear to be the same.  But its something like looking at stereo equipment, the ereg_replace() is the heavier box, much more work gone into it, lots of fancy features.  And str_replace() is the light box, still gets the job done but not as fancy... a more generic version.

Another difference of note was when I used a single quoted string all of the t's in a submission were replaced instead of the tab characters.  But when I used the double quoted string the tab replacement worked.

: )
Rich  


:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::

Reply By: nikolai Reply Date: 8/18/2003 1:08:29 PM
Well, ereg_replace searches for regular expressions, and str_replace looks for an exact match.  If you're only searching/replacing an exact match, stick with str_replace.  If you're going to use regular expressions, I'd go with preg_replace -- the preg_xxx() functions are usually faster than their ereg_xxx() counterparts!


Take care,

Nik
http://www.bigaction.org/
Reply By: nikolai Reply Date: 8/18/2003 1:10:48 PM
One more thing -- using a single quoted string tells PHP not to perform any substitutions, both variable AND escaped (backslashed) characters!  The only escapted characters that PHP recognizes in single-quoted strings are (iirc) the single-quote character and the backslash character.

http://www.php.net/types.string



Take care,

Nik
http://www.bigaction.org/
Reply By: jedhunsaker Reply Date: 11/21/2004 2:47:01 AM
cool thing about str_replace is that there's a case insensitive version.  also, you can use arrays for the search and replace...

$tab = "<span style=\"margin-left:10px;\"></span>";
$body = str_replace(array("<br>", "&amp;quot;", "\t"), array("", "&quot;", $tab), stripslashes($body));

http://us2.php.net/manual/en/function.str-ireplace.php


Go to topic 22413

Return to index page 710
Return to index page 709
Return to index page 708
Return to index page 707
Return to index page 706
Return to index page 705
Return to index page 704
Return to index page 703
Return to index page 702
Return to index page 701