[PHP] Remove Spaces from Middle of String
This works, but I was wondering if this was the best way to accomplish this or not. I'm trying to remove any extra spaces (not whitespace like carriage returns) from the middle of a string mainly for times where people put extra spaces after periods in paragraph. while(preg_match('/ /',$data)) { $data = str_replace( , ,$data);} Is there a better way to accomplish this same task? (PHP 4.x). Thanks. -- Kevin Murphy Webmaster: Information and Marketing Services Western Nevada Community College www.wncc.edu 775-445-3326
Re: [PHP] Remove Spaces from Middle of String
sender: Kevin Murphy date: Tue, Oct 03, 2006 at 11:08:23AM -0700 EOQ This works, but I was wondering if this was the best way to accomplish this or not. I'm trying to remove any extra spaces (not whitespace like carriage returns) from the middle of a string mainly for times where people put extra spaces after periods in paragraph. while(preg_match('/ /',$data)) { $data = str_replace( , ,$data);} Is there a better way to accomplish this same task? (PHP 4.x). Thanks. Sure: $ cat test.php ?=ereg_replace(' {2,}', ' ', A string with many spacesinside)? $ php -f test.php A string with many spaces inside Cheers, Alex -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove Spaces from Middle of String
If you have this, it's better: $data = preg_replace(/ /, $data); You may want to consider /\\s+/ if you want newlines to be collapsed in as well. If preg_replace is not in your PHP, so you gotta use str_replace, then strstr is better to find the ' ' because it's probably slightly faster than preg_match() I suspect preg_replace() is available everywhere preg_match() is available, but will not swear to it. On Tue, October 3, 2006 1:08 pm, Kevin Murphy wrote: This works, but I was wondering if this was the best way to accomplish this or not. I'm trying to remove any extra spaces (not whitespace like carriage returns) from the middle of a string mainly for times where people put extra spaces after periods in paragraph. while(preg_match('/ /',$data)) { $data = str_replace( , ,$data);} Is there a better way to accomplish this same task? (PHP 4.x). Thanks. -- Kevin Murphy Webmaster: Information and Marketing Services Western Nevada Community College www.wncc.edu 775-445-3326 -- Some people have a gift link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove Spaces from Middle of String
On Tue, October 3, 2006 1:47 pm, Richard Lynch wrote: If you have this, it's better: $data = preg_replace(/ /, $data); D'oh! $data = preg_replace(/ /, , $data); Sorry! -- Some people have a gift link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove Spaces from Middle of String
On Tue, 2006-10-03 at 15:17 -0500, Richard Lynch wrote: On Tue, October 3, 2006 1:47 pm, Richard Lynch wrote: If you have this, it's better: $data = preg_replace(/ /, $data); D'oh! $data = preg_replace(/ /, , $data); Doesn't work on 3+ spaces in a row ;) $data = preg_replace(/ +/, , $data); Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove Spaces from Middle of String
Works perfect! Thanks. -- Kevin Murphy Webmaster: Information and Marketing Services Western Nevada Community College www.wncc.edu 775-445-3326 On Oct 3, 2006, at 3:30 PM, Robert Cummings wrote: On Tue, 2006-10-03 at 15:17 -0500, Richard Lynch wrote: On Tue, October 3, 2006 1:47 pm, Richard Lynch wrote: If you have this, it's better: $data = preg_replace(/ /, $data); D'oh! $data = preg_replace(/ /, , $data); Doesn't work on 3+ spaces in a row ;) $data = preg_replace(/ +/, , $data); Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `'