Wrox Programmer Forums
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 21st, 2005, 10:50 AM
Authorized User
 
Join Date: Dec 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Removing spaces

I'd like to remove spaces from a text string. I know how to do it the old-fashioned way, but I thought I'd ask if there wasn't a PHP function something akin to strstripspaces($string)that would make life easier.

Thanks!

 
Old October 27th, 2005, 05:26 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 357
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anshul
Default

See this simple example:
Code:
<?php
$data0 = "Sadly,  PHP  is  an   interpreted  slow language,    is  the  tool  of  freelancers,     not  corporates.";
// whitespaces of 2, 2, 2, 3, 2, 2, 4, 2, 2, 2, 2, 5, 2
$data1 = preg_replace('/\s\s+/', '', $data0); // remove all whitespace patterns
$data2 = preg_replace('/\s\s+/', ' ', $data0); // replace all whitespace patterns with only one space

echo "<br>data0: " . $data0 . "<br>";
echo "<br>data1: " . $data1 . "<br>";
echo "<br>data2: " . $data2 . "<br>";
?>
`~@#\^%&*/\.<.\/-|+|_!:;..=?>
Support Indian students' finances http://scholarship.mediasworks.com/
 
Old October 28th, 2005, 12:02 PM
Authorized User
 
Join Date: Dec 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

See?

I could have spent a long, long time searching for preg_replace.

Thank you!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Spaces XP Programmer911 General .NET 1 December 19th, 2007 07:03 AM
Diference between these two name spaces pradeepn XSLT 1 June 25th, 2007 02:31 AM
Removing Blank Spaces in Phone Numbers Brendan Bartley Access 1 April 12th, 2007 11:42 AM
disappearing spaces gezi XSLT 0 March 26th, 2006 02:23 AM
spaces problem mateenmohd SQL Server 2000 8 August 11th, 2003 06:27 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.