thehigherentity Posted June 7, 2006 Share Posted June 7, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/11384-remove-excessive-spaces-from-anywere-in-a-string/ Share on other sites More sharing options...
.josh Posted June 7, 2006 Share Posted June 7, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/11384-remove-excessive-spaces-from-anywere-in-a-string/#findComment-42667 Share on other sites More sharing options...
poirot Posted June 7, 2006 Share Posted June 7, 2006 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 Link to comment https://forums.phpfreaks.com/topic/11384-remove-excessive-spaces-from-anywere-in-a-string/#findComment-42672 Share on other sites More sharing options...
thehigherentity Posted June 7, 2006 Author Share Posted June 7, 2006 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 throughextract($_POST); // I will be changing that when I have it working properlyall 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 thereI 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! Quote Link to comment https://forums.phpfreaks.com/topic/11384-remove-excessive-spaces-from-anywere-in-a-string/#findComment-42673 Share on other sites More sharing options...
.josh Posted June 7, 2006 Share Posted June 7, 2006 bah. okay i'm retarded. Quote Link to comment https://forums.phpfreaks.com/topic/11384-remove-excessive-spaces-from-anywere-in-a-string/#findComment-42676 Share on other sites More sharing options...
trq Posted June 7, 2006 Share Posted June 7, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/11384-remove-excessive-spaces-from-anywere-in-a-string/#findComment-42688 Share on other sites More sharing options...
.josh Posted June 7, 2006 Share Posted June 7, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/11384-remove-excessive-spaces-from-anywere-in-a-string/#findComment-42706 Share on other sites More sharing options...
trq Posted June 7, 2006 Share Posted June 7, 2006 [!--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. Quote Link to comment https://forums.phpfreaks.com/topic/11384-remove-excessive-spaces-from-anywere-in-a-string/#findComment-42708 Share on other sites More sharing options...
.josh Posted June 7, 2006 Share Posted June 7, 2006 yah me too :( Quote Link to comment https://forums.phpfreaks.com/topic/11384-remove-excessive-spaces-from-anywere-in-a-string/#findComment-42717 Share on other sites More sharing options...
poirot Posted June 7, 2006 Share Posted June 7, 2006 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]<?phpfunction 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: 1MBMethod 1: 0.767633Method 2: 0.053614Size: 2MBMethod 1: 1.923115Method 2: 0.111017Size: 4MBMethod 1: 5.752348Method 2: 0.229851////////////////////Lots of extra spaces////////////////////Size: 1MBMethod 1: 1.286320Method 2: 0.038153Size: 2MBMethod 1: 2.726342Method 2: 0.075625Size: 4MBMethod 1: 6.157305Method 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. ;) Quote Link to comment https://forums.phpfreaks.com/topic/11384-remove-excessive-spaces-from-anywere-in-a-string/#findComment-42942 Share on other sites More sharing options...
Koobi Posted June 7, 2006 Share Posted June 7, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/11384-remove-excessive-spaces-from-anywere-in-a-string/#findComment-42954 Share on other sites More sharing options...
trq Posted June 7, 2006 Share Posted June 7, 2006 [!--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. Quote Link to comment https://forums.phpfreaks.com/topic/11384-remove-excessive-spaces-from-anywere-in-a-string/#findComment-42996 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.