![]() |
PHP Whitespace Replacement
Hello All!
I need something that will allow me to replace each space character with but only when the amount of space characters is greater than (or equal to) 2. For example: Code:
This had 5 spacesCode:
This had 5 spacesPHP Code:
Edit: Sorry, I only mean space. As in, " ". |
There are much better ways to preserve preformatted text, I'm sure. A line of nbsp's is a good way to tell you're doing something wrong. I suppose the proper method depends on what's happening with the data before it reaches the page (Passing through a database, etc).
|
try escaping it and also you might try \s
PHP Code:
|
Quote:
PHP Code:
|
Quote:
Quote:
Would it work if I found all the consecutive spaces, count how many consecutive spaces there were, stored that number in a variable, and then called the replacement that many times? |
Quote:
PHP Code:
|
Quote:
Your original data will still be stored with the number of spaces you require. Don't store them with the HTML non-breaking space. You use the preformat CSS to retain them in HTML since it ignores multiple whitespace. |
So, the whitespace isn't being collapsed until it reaches the HTML, providing it's in a element that isn't
white-space: pre? |
That's correct. HTML is what collapses the whitespace, the db and PHP will happily store it. You should even be able to see it in the HTML source code when printed from PHP, but it won't render the multiple spaces.
|
Well, there you go shadowsai. You see these code blocks:
Code:
All you need is: PHP Code:
|
Quote:
Thanks! I can't believe that one slipped my mind. Yeah, I knew that it was the HTML that was causing the spaces to be collapsed, but couldn't think of something other than replacing everything with . |
| All times are GMT +1. The time now is 08:52 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.