 |
| PHP How-To Post your "How do I do this with PHP?" questions here. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 1st, 2003, 09:19 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
|
|
Getting tab characters to show up in output
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: [email protected]
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
|
|

July 2nd, 2003, 04:13 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
Code:
echo "<pre>word1\tword2</pre>";
Or, you can use several instead of span tag if you want to simulate TAB's displaying.
Regs,
...but the Soon is eclipsed by the Moon
|
|

July 2nd, 2003, 08:42 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
|
|
>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 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
:::::::::::::::::::::::::::::::::
|
|

July 2nd, 2003, 02:20 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Usually, I use a combination of spaces and non-breaking spaces to simulate a tab-like visual effect.
Code:
<?php
$TAB = " ";
echo "Hello,{$TAB}world.\n";
?>
Take care,
Nik
http://www.bigaction.org/
|
|

July 3rd, 2003, 02:26 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
u sure u did all that before u echoed $body?
----------------------------
http://aeonofdarkness.com
|
|

July 5th, 2003, 04:15 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
|
|
>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('&quot;', '"', $body);
$body = str_replace('\t', $tab, $body);
echo nl2br($body);
: )
Rich
:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
|
|

August 11th, 2003, 09:46 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
|
|
*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
:::::::::::::::::::::::::::::::::
|
|

August 18th, 2003, 01:08 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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/
|
|

August 18th, 2003, 01:10 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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/
|
|

November 21st, 2004, 03:47 AM
|
|
Registered User
|
|
Join Date: Nov 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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>", "&quot;", "\t"), array("", """, $tab), stripslashes($body));
http://us2.php.net/manual/en/function.str-ireplace.php
|
|
 |