473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Removing extra spaces between words

code green
1,726 Expert 1GB
I am trying to write a simple function that will take a string containing an
address line or business name and return it nicely formatted.
By this I mean extra spaces removed and words capitalised.
I also wish it to be legal XML..
The result string is further sent to another function to check
the names and addresses don't appear on a blacklist
which is why the extra spaces need removing.
This is my test function so far
[PHP]function cleanProperNouns($name)
{
$words = explode(' ',strtolower($name));
echo '<br>Before';
print_r($words);

foreach($words as &$w)
$w = trim($w);

echo '<br>After';
print_r($words);
return htmlspecialchars(ucwords(implode(' ',$words)));
}

echo cleanProperNouns('this & is a test name '); [/PHP] It does not remove all the extra spaces in the string.
It looks fine in a browser but source is not.
Has anybody written a function that does something similar or got any better ideas?
Jul 22 '08 #1
2 10308
Atli
5,058 Expert 4TB
Hi.

You could try regular expressions. Like:
Expand|Select|Wrap|Line Numbers
  1. preg_replace('/ +/', ' ', $str);
  2.  
This simply looks for a one or more spaces and replaces them all with a single space.

You could of course write a old fashion function that does the exact thing.
It would basically just have to look for a the first occurrence of a white-space, count the number of white-spaces that follow the first one, and replace the lot with a single space.
Jul 22 '08 #2
code green
1,726 Expert 1GB
Well done Atli for shocking my brain back into life.
I'm struggling with trim instead of str_replace, preg_replace.
What I was concentrating on was replacing the little loop with an array function / string function combination to clean the data.
I am not 100% sure what all those array functions do and thought maybe somebody has a clever alternative to looping through the array
Jul 23 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Marco Gallo | last post by:
I've been trying to get rid of extra spaces in a table that I created with addresses in it. For instance in a field called TEST, I got the following address: " 1 main st Apt A "...
11
by: gopal srinivasan | last post by:
Hi, I have a text like this - "This is a message containing tabs and white spaces" Now this text contains tabs and white spaces. I want remove the tabs and white...
12
by: Magix | last post by:
Hi, Everytime I received a fix-length of string, let say 15 (the unused portion will filled with Spaces before receive), I want to remove the Spaces from END until I encounter a non-space char....
3
by: Ray Stevens | last post by:
Does anyone know of a Regex expression from removing extra whitespace within a string (such as 5 spaces instead of 1, etc.)?
4
by: dave | last post by:
I have a csss/html question that perhaps the experts here can help me with. The following code leaves small gaps between the div tags. how can I remove them? <div > <div > <div <div > <img...
5
by: dw | last post by:
hello - first, let me state that i am an Asp.Net rookie. here is the situation: i have a page that looks good in the vs.net designer, but when the page renders there are extra amounts...
8
by: starsky51 | last post by:
I'm sure it's something i'm doing wrong, I just can't see it. I've set up a simple page with the following code: <html> <head> <title>tester</title> <script language="javascript"...
2
by: beatTheDevil | last post by:
Hey guys, As the title says I'm trying to make a regular expression (regex/regexp) for use in removing the comments from code. In this case, this particular regex is meant to match /* ... */...
11
by: George Sakkis | last post by:
I have a situation where one class can be customized with several orthogonal options. Currently this is implemented with (multiple) inheritance but this leads to combinatorial explosion of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.