Jump to content

Recommended Posts

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
umm.. can you post some code or something? cuz when you post input fields or even text areas, the spaces are automatically removed.

example:
[code]
<?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>
[/code]
this will echo out:
[b]Today I ate a cheeseburger and french fries and a shake.[/b]

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

so..if you are having issues with it, is the code you already have, somehow preserving those spaces in the first place?
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) [snapback]380867[/snapback][/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

[code]$newstring = preg_replace("/ {2,}/", " ", $oldstring);[/code]
[/quote]
Thanks for that I will give it a go later today!
Probably more efficient ways but....
[code]
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"));
[/code]
[!--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.
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:

[code]<?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);

?>[/code]

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

[code]////////////////////
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[/code]

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. ;)
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.