Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Dec 2009
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder Custard7A's Avatar
    Join Date
    Jul 2010
    Location
    Australia
    Posts
    286
    Thanks
    32
    Thanked 33 Times in 33 Posts
    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).

  • #3
    Junsee
    Guest
    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 " "

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #5
    New Coder
    Join Date
    Dec 2009
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,302
    Thanks
    4
    Thanked 204 Times in 201 Posts
    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

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #8
    Regular Coder Custard7A's Avatar
    Join Date
    Jul 2010
    Location
    Australia
    Posts
    286
    Thanks
    32
    Thanked 33 Times in 33 Posts
    So, the whitespace isn't being collapsed until it reaches the HTML, providing it's in a element that isn't white-space: pre?

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • Users who have thanked Fou-Lu for this post:

    Custard7A (12-12-2012)

  • #10
    Regular Coder Custard7A's Avatar
    Join Date
    Jul 2010
    Location
    Australia
    Posts
    286
    Thanks
    32
    Thanked 33 Times in 33 Posts
    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>'

  • #11
    New Coder
    Join Date
    Dec 2009
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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;.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •