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 spaces
Would become this.
Code:
This had 5 spaces
I thought of using regex, like below, but it doesn't allow me to match each space character. I don't know of a way to "count" the amount of matched characters.
PHP Code:
preg_replace("/[ ]{2,}/", " ", $string)
Any help is appreciated!
Edit: Sorry, I only mean space. As in, " ".
Last edited by shadowsai; 12-10-2012 at 07:06 AM..
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).
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).
Agreed. Use CSS for your markup; assuming this is coming from some storage or input, you likely have an intent to display it in some relevant element block. Its as simple as forcing a pre whitespace:
Patterning this one is more of a pain than it looks. It would be trivial to replace multiple whitespace with a single , but more complex to perform many replacements for a single character that is in a multiple sequence. Use the preformat instead as its a lot easier and you needn't worry about the data for the medium that you are using.
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).
Yes, I know that, however, I need to display all those extra spaces to preserve the formatting in each line.
Quote:
Originally Posted by Fou-Lu
Agreed. Use CSS for your markup; assuming this is coming from some storage or input, you likely have an intent to display it in some relevant element block. Its as simple as forcing a pre whitespace:
Patterning this one is more of a pain than it looks. It would be trivial to replace multiple whitespace with a single , but more complex to perform many replacements for a single character that is in a multiple sequence. Use the preformat instead as its a lot easier and you needn't worry about the data for the medium that you are using.
Well, the problem is, I need to retain (and display) the original amount of spaces in the database. I can easily replace every single occurrence of " ", but I have no idea how to do this. The database structure exists (in MSAccess), I'm just converting the entire application to MySQL/PHP.
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?
Doing that it doesn't matter if there is one space or 5 spaces. They will all be changed to a required space.
This will also replace all spaces which may not be desired.
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.
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.
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 .