CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   PHP Whitespace Replacement (http://www.codingforums.com/showthread.php?t=283882)

shadowsai 12-10-2012 07:02 AM

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 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, " ".

Custard7A 12-10-2012 09:44 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).

Junsee 12-10-2012 01:30 PM

try escaping it and also you might try \s

PHP Code:

preg_replace("#[\ ]{2,}#"" "$string

preg_replace("#[\s]{2,}#"" "$string

however \s will match all whitespace like a space and a tab will become " "

Fou-Lu 12-10-2012 03:31 PM

Quote:

Originally Posted by Custard7A (Post 1298689)
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:
PHP Code:

$s 'This     had 5 spaces';

printf('<span style="white-space: pre;">%s</span>'$s); 

Patterning this one is more of a pain than it looks. It would be trivial to replace multiple whitespace with a single &nbsp;, 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.

shadowsai 12-11-2012 07:21 AM

Quote:

Originally Posted by Custard7A (Post 1298689)
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 (Post 1298747)
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:
PHP Code:

$s 'This     had 5 spaces';

printf('<span style="white-space: pre;">%s</span>'$s); 

Patterning this one is more of a pain than it looks. It would be trivial to replace multiple whitespace with a single &nbsp;, 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?

djm0219 12-11-2012 12:45 PM

Quote:

Originally Posted by shadowsai (Post 1298991)
I can easily replace every single occurrence of " ", but I have no idea how to do this.

PHP Code:

$original 'This     had 5 spaces';
$fixed    str_replace(' ','&nbsp;',$original);
echo 
$fixed

Doing that it doesn't matter if there is one space or 5 spaces. They will all be changed to a required space.

Fou-Lu 12-11-2012 01:51 PM

Quote:

Originally Posted by djm0219 (Post 1299048)
PHP Code:

$original 'This     had 5 spaces';
$fixed    str_replace(' ','&nbsp;',$original);
echo 
$fixed

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.

Custard7A 12-11-2012 11:26 PM

So, the whitespace isn't being collapsed until it reaches the HTML, providing it's in a element that isn't white-space: pre?

Fou-Lu 12-12-2012 01:33 AM

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.

Custard7A 12-12-2012 01:54 AM

Well, there you go shadowsai. You see these code blocks:

Code:



This    had 5 spaces

They aren't replacing any conjoining spaces with nbsp, just outputting the text into a pre element.

All you need is:

PHP Code:



$original 
'This     had 5 spaces'
echo 
'<pre>' $original '</pre>'


shadowsai 12-12-2012 03:52 AM

Quote:

Originally Posted by Custard7A (Post 1299206)
Well, there you go shadowsai. You see these code blocks:

Code:



This    had 5 spaces

They aren't replacing any conjoining spaces with nbsp, just outputting the text into a pre element.

All you need is:

PHP Code:



$original 
'This     had 5 spaces'
echo 
'<pre>' $original '</pre>'



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 &nbsp;.


All times are GMT +1. The time now is 08:52 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.