Jump to content


Photo

remove excessive spaces from anywere in a string


  • Please log in to reply
11 replies to this topic

#1 thehigherentity

thehigherentity
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 07 June 2006 - 04:29 AM

I know this is probably really easy but I can’t seem to get my head round “preg_replace” Im trying to remove excessive spaces, so if someone’s input has more then one space anywhere in the text (not just at the beginning or the end ) it will remove the extras and leave only one.

I know trim () will do the leading and end ones but I need it so any more then one space, anywhere in the text is not allowed! And will be changed back to one single space [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]

If someone could help me with this I would be very grateful

The best way to learn
is to ask you lot........

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 07 June 2006 - 04:55 AM

umm.. can you post some code or something? cuz when you post input fields or even text areas, the spaces are automatically removed.

example:
<?php
   $blah = "   Today I    ate a cheeseburger     and french         fries and a shake.   ";
   echo $blah."<br><br>";
   if($_POST['blahtext']) {
        echo $_POST['blahtext']."<br><br>";
     }
   if($_POST['blahfield']) {
        echo $_POST['blahfield'];
     }
?>

<form method='post' action='<?=$PHP_SELF ?>'>
<input type='text' name='blahfield'><br>
<textarea name='blahtext' rows="5" cols="20">
</textarea>
<input type='submit'>
</form>
this will echo out:
Today I ate a cheeseburger and french fries and a shake.

and if i were to enter in
alsdkf          asdlfkj        lkkd          skk 
into the textfield and/or the textarea it will echo out:
alsdkf asdlfkj lkkd skk

so..if you are having issues with it, is the code you already have, somehow preserving those spaces in the first place?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 07 June 2006 - 05:08 AM

Crayon, they are not removed. They are merely not displayed by the browser, but if you put <pre> tags you will see them.

You might want to use

$newstring = preg_replace("/ {2,}/", " ", $oldstring);

~ D Kuang

#4 thehigherentity

thehigherentity
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 07 June 2006 - 05:15 AM

Im not on the right computer at the moment so I cant post the code but…
I can tell you it’s a standard text area, which I am collecting the information through
extract($_POST); // I will be changing that when I have it working properly

all I have is echo $text; and on the html it looks fine but when I look at the code there is extra spaces.(well there is when I add then to the text area). The text is displayed back into the text area with a simple echo and the spaces are in the text box too;

This is why I striped the code down to its bones and I still find the spaces are there
I need to compare some of the input to existing information in my database so I need to remove these spaces.

If I echo the information to screen it only shows the one space but like I say in the actual source code there are more


[!--quoteo(post=380867:date=Jun 7 2006, 12:08 AM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ Jun 7 2006, 12:08 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Crayon, they are not removed. They are merely not displayed by the browser, but if you put <pre> tags you will see them.

You might want to use

$newstring = preg_replace("/ {2,}/", " ", $oldstring);
[/quote]
Thanks for that I will give it a go later today!
The best way to learn
is to ask you lot........

#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 07 June 2006 - 05:37 AM

bah. okay i'm retarded.


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 07 June 2006 - 06:38 AM

Probably more efficient ways but....
function deeptrim($string) {
    $string = trim($string);
    $tmp = explode(" ",$string);
    foreach($tmp as $key => $val) {
        if (!$val == "") {
            $return[] = $val;
        }
    }
    return implode(" ",$return);
}

echo (deeptrim("here is a        string with          some bigger    spaces in     it"));


#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 07 June 2006 - 07:34 AM

yeh i uh, pretty much came up with that exact same thing earlier (except that i didn't put it in a function). but poirot's code does work and it's only 1 line so clearly it is the better solution.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#8 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 07 June 2006 - 07:37 AM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]poirot's code does work and it's only 1 line so clearly it is the better solution.[/quote]
Hehe.... I didn't even see it. Of course its a better solution. Ive so gotta get around to learning more about regex's.

#9 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 07 June 2006 - 08:07 AM

yah me too :(
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#10 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 07 June 2006 - 08:48 PM

To not jump into conclusion; like this is one line so it should be faster, I decided to do some benchmarking. The code used was:

<?php

function deeptrim($string) {
    $string = trim($string);
    $tmp = explode(" ",$string);
    $return = array(); // Had to add this bc it was not working
    foreach($tmp as $key => $val) {
        if (!$val == "") {
            $return[] = $val;
        }
    }
    return implode(" ",$return);
}

function deeptrim2($str)
{
    return preg_replace("/ {2,}/", " ", $str);
}

$str = file_get_contents('dummy.txt');

$time1 = microtime(true);
$str1 = deeptrim($str);

$time2 = microtime(true);

$str2 = deeptrim2($str);
$time3 = microtime(true);

echo 'Method 1: ' . number_format($time2 - $time1, 6)  . '<br />';
echo 'Method 2: ' . number_format($time3 - $time2, 6);

?>

dummy.txt is a very large dummy file. I created different dummies with different sizes. The results:

////////////////////
No extra spaces
////////////////////

Size: 1MB
Method 1: 0.767633
Method 2: 0.053614

Size: 2MB
Method 1: 1.923115
Method 2: 0.111017

Size: 4MB
Method 1: 5.752348
Method 2: 0.229851

////////////////////
Lots of extra spaces
////////////////////

Size: 1MB
Method 1: 1.286320
Method 2: 0.038153

Size: 2MB
Method 1: 2.726342
Method 2: 0.075625

Size: 4MB
Method 1: 6.157305
Method 2: 0.154641

Although sizes near 1MB are a bit unrealistic, using Regex will be way faster, as the size grows the difference is more visible. Also, while the time increases linearly using regex, it will increase exponentially using the other method. ;)
~ D Kuang

#11 Koobi

Koobi
  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 07 June 2006 - 09:10 PM

i think a lot of the time goes towards iterating through that foreach. if you used something like array_walk() for example, it mightbe faster but i don't know by how much.

#12 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 07 June 2006 - 10:50 PM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]i think a lot of the time goes towards iterating through that foreach[/quote]
Yeah... loops are bad mkay.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users