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
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post

    Deleting 2 or more spaces with preg_replace() ?

    I'm still a RegEx n00b & can't work out how to do this; I tried the below but it didn't seem to work.

    PHP Code:
    $line2 preg_replace('/ +/'' '$line); 
    Thanks!

  • #2
    Regular Coder xconspirisist's Avatar
    Join Date
    Jun 2006
    Location
    Great Britain.
    Posts
    138
    Thanks
    1
    Thanked 6 Times in 6 Posts
    I believe your regex is okay, you might want to check if you are assigning variables appropriatly. Note that even if a line of text contains many spaces, most web browsers will only render a single space. I still think your code could be a assignment error though. Never the less, here is some code that will work:

    PHP Code:
    $l 'This  is  a      line     of text.';
    $l preg_replace('# +#'' '$l);
    echo 
    $l
    If I have been helpful, use the "thank" button - It makes me happy!

    xconspirisist.co.uk - homepage of my online alias
    technowax.net - a community for people interested in all forms of modern technology.

  • #3
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Hmmm...... well here is my full code so you can see:

    PHP Code:
    $file file('org.txt');

    foreach (
    $file as $line) {

     
    $line trim($line);

     
    $line2 preg_replace('/  */'' '$line);

     echo 
    "$line<br>$line2<p>\n";

    exit();


    The "echo ()" & the "exit()" are just there for testing purposes. As you say, yeah... the browser display is fine; but when I look in the source both outputs are still the same & $line2 still contains big spaces.

    Here is an example line from "org.txt"..

    PHP Code:
    activitedechezsoi.org    2006-12-31            765    0    0    0    0    11    2005 
    Cheers!

  • #4
    Regular Coder xconspirisist's Avatar
    Join Date
    Jun 2006
    Location
    Great Britain.
    Posts
    138
    Thanks
    1
    Thanked 6 Times in 6 Posts
    You now have two spaces in your regex pattern, and a *. You need one space and a + as per my example. Try using hash's to serpate your pattern too instead of forward slashes. It could be the fact that you are using file. Not sure. try file_get_contents($fileName) instead.

    Also, you cannot use foreach to itterate over a string. You should do this before if you want to use a foreach:

    PHP Code:
    $file explode("\n"$file); 
    This also presumes your file is in unix format.
    Last edited by xconspirisist; 12-30-2006 at 12:14 AM. Reason: Added comment about using foreach to itterate over a string.
    If I have been helpful, use the "thank" button - It makes me happy!

    xconspirisist.co.uk - homepage of my online alias
    technowax.net - a community for people interested in all forms of modern technology.

  • #5
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Sorry...... that wa sjust another way I was testing it cause the method I posted above didn't work.

    As for the "foreach()"; I am using that to iterate over each line of the file & why file_get_contents() won't work as it won't sort it into lines; only one complete string.

  • #6
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    the foreach might be your problem, perhaps post the real code that doesn't work, if the following doesn't help?

    When you use 'as $something' in the foreach loop, $something isn't a reference to the array-value, so making changes to it will 'be lost' once the loop is finished.
    An example
    PHP Code:
    $array = array('a','b','c','d');
    foreach(
    $array as $item) {
      
    $item='xyz';
    }
    echo 
    $array[1]; //will output 'b', not 'x' 
    one solution:
    PHP Code:
    $array = array('a','b','c','d');
    foreach(
    $array as $key=>$value) {
      
    $array[$key]='x';
    }
    echo 
    $array[1]; //will echo 'x' now 
    by using the $key=>$value optons with foreach, it allows you to refer back to the actual array, and make changes to it.

    Is this what's happening with your loop?

  • #7
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Try
    Code:
    preg_replace('/\s+/',' ', $text);
    Last edited by marek_mar; 12-30-2006 at 12:58 AM.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #8
    Regular Coder xconspirisist's Avatar
    Join Date
    Jun 2006
    Location
    Great Britain.
    Posts
    138
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Ah, I feel like a complete arse, I can't believe I didn't spot it! What GJay said was correct. foreach assigns the seccond argument by copying, not by reference. His example is correct and should solve your problem.
    If I have been helpful, use the "thank" button - It makes me happy!

    xconspirisist.co.uk - homepage of my online alias
    technowax.net - a community for people interested in all forms of modern technology.

  • #9
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Quote Originally Posted by marek_mar View Post
    Try
    Code:
    preg_replace('/\s+/',' ', $text);
    Thank you so much..... tried everything but that was the only thing that worked!

  • #10
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I had a feeling that doing something slightly different from what you asked will do the trick.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #11
    New Coder
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    49
    Thanks
    0
    Thanked 3 Times in 3 Posts
    nv...


  •  

    Posting Permissions

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