Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-10-2012, 07:02 AM   PM User | #1
shadowsai
New Coder

 
Join Date: Dec 2009
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
shadowsai is an unknown quantity at this point
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, " ".

Last edited by shadowsai; 12-10-2012 at 07:06 AM..
shadowsai is offline   Reply With Quote
Old 12-10-2012, 09:44 AM   PM User | #2
Custard7A
Regular Coder

 
Custard7A's Avatar
 
Join Date: Jul 2010
Location: Australia
Posts: 269
Thanks: 32
Thanked 32 Times in 32 Posts
Custard7A is an unknown quantity at this point
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).
Custard7A is offline   Reply With Quote
Old 12-10-2012, 01:30 PM   PM User | #3
Junsee
New Coder

 
Join Date: Jun 2012
Posts: 42
Thanks: 4
Thanked 5 Times in 5 Posts
Junsee is an unknown quantity at this point
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 " "
Junsee is offline   Reply With Quote
Old 12-10-2012, 03:31 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by Custard7A View Post
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.
Fou-Lu is offline   Reply With Quote
Old 12-11-2012, 07:21 AM   PM User | #5
shadowsai
New Coder

 
Join Date: Dec 2009
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
shadowsai is an unknown quantity at this point
Quote:
Originally Posted by Custard7A View Post
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 View Post
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?
shadowsai is offline   Reply With Quote
Old 12-11-2012, 12:45 PM   PM User | #6
djm0219
Senior Coder

 
djm0219's Avatar
 
Join Date: Aug 2003
Location: Wake Forest, North Carolina
Posts: 1,227
Thanks: 2
Thanked 189 Times in 187 Posts
djm0219 is on a distinguished road
Quote:
Originally Posted by shadowsai View Post
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.
__________________
Dave .... HostMonster for all of your hosting needs
djm0219 is offline   Reply With Quote
Old 12-11-2012, 01:51 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by djm0219 View Post
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.
Fou-Lu is offline   Reply With Quote
Old 12-11-2012, 11:26 PM   PM User | #8
Custard7A
Regular Coder

 
Custard7A's Avatar
 
Join Date: Jul 2010
Location: Australia
Posts: 269
Thanks: 32
Thanked 32 Times in 32 Posts
Custard7A is an unknown quantity at this point
So, the whitespace isn't being collapsed until it reaches the HTML, providing it's in a element that isn't white-space: pre?
Custard7A is offline   Reply With Quote
Old 12-12-2012, 01:33 AM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Custard7A (12-12-2012)
Old 12-12-2012, 01:54 AM   PM User | #10
Custard7A
Regular Coder

 
Custard7A's Avatar
 
Join Date: Jul 2010
Location: Australia
Posts: 269
Thanks: 32
Thanked 32 Times in 32 Posts
Custard7A is an unknown quantity at this point
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>'
Custard7A is offline   Reply With Quote
Old 12-12-2012, 03:52 AM   PM User | #11
shadowsai
New Coder

 
Join Date: Dec 2009
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
shadowsai is an unknown quantity at this point
Quote:
Originally Posted by Custard7A View Post
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;.
shadowsai is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


Advertisement
Log in to turn off these ads.