Click to See Complete Forum and Search --> : Need some help with my PHP code.


OM2
01-12-2007, 12:47 PM
I'm trying to write some PHP code to:

- Take an address and properly format it
- It needs to lower case everything
- All the first letters of every word need to be capitals
- Replace ',' with a new line character

I've had a little bit of success... but am stuck because it doesn't have the desired results.

See the following code:


<html>
<body>

<?php
$addressString = strtolower($_POST["address"]);
$addressString = ucwords($addressString);
$addressString = str_replace(",", "\n", $addressString);
?>

<form action="test3.php" method="post" enctype="multipart/form-data" name="addressFixer">

<textarea name="address" cols="40" rows="6">
<?php echo $addressString ?>
</textarea>

<br>
<br>
<label>
<input type="submit" name="Submit" value="Submit">
</label>
</form>

</body>
</html>


(The file is called test3.php... so calls itself.)

Problems:

- When the above code is run, space characters are inserted at the begining.
Have no idea why this is. :(

- The last line will always be a Post Code.
Just this line needs to be treated differently.
All letters need to be capitals. And there needs to be a space inserted before the last 3 letters.

- Also I need to have any extra spaces trimmed.

1. I can't figure out how to get rid of the first problem.
2. I'm scratching my head how to best seperate just the last line to treat differently because it's a Post Code.
3. I'm sure the best approach here is to use trim functions... but not sure this will help if I'm working with only one string in the first place.

Help... I'm stuck! :(

Any help would be appreciated.

Thanks.


OM

balloonbuffoon
01-12-2007, 12:52 PM
Give a sample of what it should look like before formatting, and then an example of it formatted.

--Steve

OM2
01-12-2007, 01:11 PM
Well... here's a sample of what i might start off with:

David ALAN
5 SOUTH mills
NEW YORK,STATE , nEW HAMSHIRE,
York, North Yorkshire
YO346NR

The correct address using the above would be:

David Alan
5 South mills
New York
State
New Hamshire
York
North Yorkshire
Y034 6NR

Comments on what I did to fix:

- Got rid of any double (or more) spaces and replace them with single ones
- The ',' Character was replaced with a new line
- Extra spaces at the begining of lines were also removed
- A space was inserted before the last 3 characters of the Post Code
- Any letter 'o' in the Post Code was replaced in the Post Code with 0 (that's a number 0).

Erm... I think that's about everything. (I know I added a few more things than originally stated!)

Having trouble getting my head round the best way to code this.

I was thinking:

- Replace all ',' characters with a newline character
- Read all lines into an array

I think it might be easier to deal with if I read all the lines into an array.
I've done lots of searching (well a little anyway) and I can't figure out how to read lines into an array.

Or is it possible to do what I need without reading into an array?

Any help would be good.

Thanks.


OM

NogDog
01-12-2007, 02:43 PM
To fix the first problem:

<textarea name="address" cols="40" rows="6"><?php
echo $addressString;
?></textarea>

(Any white-space between the <textarea> and </textarea> tags not within the <?php ... ?> tags will be treated as spaces within the textarea.)

OM2
01-12-2007, 08:22 PM
aaaaaaaaaaaaaargh!
u wont believe how long i've struggled with getting rid of extra spaces that appeared in the input area!

i think i've solved it.
i have one problem: how do i get rid of the newline character at the end of the string?

i've got the code working... but am having trouble getting rid of the \n character i put in at the end of each line.

see below for my code:

<html>
<body>

<?php
$addressString = strtolower($_POST["address"]);
$addressString = str_replace("\n", ",", $addressString);
$myArray = explode(",", $addressString);

$numberOfArrayItems = count($myArray);

$myArray[$numberOfArrayItems - 1] = strtoupper($myArray[$numberOfArrayItems - 1]);

$newCorrectFormatAddress = "";

for ($i=0; $i<=$numberOfArrayItems; $i++)
{
$myArray[$i] = ucwords($myArray[$i]);
$myArray[$i] = trim($myArray[$i]);
$newCorrectFormatAddress = $newCorrectFormatAddress . $myArray[$i] . "\n";
}
?>

<form action="test4.php" method="post" name="addressFixer">

<textarea name="address" cols="40" rows="6"><?php echo $newCorrectFormatAddress; ?></textarea>

<br>
<br>

<label>
<input type="submit" name="Submit" value="Submit">
</label>
</form>

</body>
</html>

thanks.

NogDog
01-12-2007, 08:45 PM
You can use trim() to get rid of any leading/trailing white-space (including newlines), or rtrim() if you just want to trim the end of the string.

OM2
01-13-2007, 07:21 PM
i finally got what i wanted to work! :)
thanks for all the replies.
i think i may have been stuck for a few more days without some of the simple replies!

the code i ended up with is:

<html>
<body>

<?php
$addressString = strtolower($_POST["address"]);
$addressString = str_replace("\n", ",", $addressString);
$myArray = explode(",", $addressString);

$numberOfArrayItems = count($myArray);

$myArray[$numberOfArrayItems - 1] = strtoupper($myArray[$numberOfArrayItems - 1]);

$newCorrectFormatAddress = "";

for ($i=0; $i<=$numberOfArrayItems; $i++)
{
$myArray[$i] = ucwords($myArray[$i]);
$myArray[$i] = trim($myArray[$i]);
$newCorrectFormatAddress = $newCorrectFormatAddress . $myArray[$i] . "\n";
}

$newCorrectFormatAddress = trim($newCorrectFormatAddress);
?>

<form action="test5.php" method="post" name="addressFixer">

<textarea name="address" cols="40" rows="6"><?php echo $newCorrectFormatAddress; ?></textarea>

<br>
<br>

<label>
<input type="submit" name="Submit" value="Submit">
</label>
</form>

</body>
</html>

i decided not to do anything with post codes in the end... apart from that everything else is as i wanted.

*only* one small glitch that i can't work out.
i put the following address through:

David Allen
20 L'Oriel Road
London, EC1 2AA

The aprostophe is given a \ in front of it and I get the following:

David Allen
20 L\'oriel Road
London
EC1 2AA

What's happening?
I can't see anything wrong with the code?

AND: I'd really appreciate ANY comments on the actual code.
Are there any parts of that could have been done in a more optimal way?

Thanks.

NogDog
01-13-2007, 08:27 PM
Sounds like magic quotes (http://www.php.net/manual/en/ref.info.php#ini.magic-quotes-gpc) are on.

<?php
$addressString = strtolower($_POST["address"]);
if(get_magic_quotes_gpc())
{
$addressString = stripslashes($addressString);
}
// rest of code...
?>

netbuddy
01-13-2007, 08:36 PM
I would like to point out that strtolower(... which is wrapped around the email field data is a bad thing to do.

What about all those people who have upper case addresses?

My.Email@somewhere.com is not the same address as my.email@somewhere.com so blokey at My.Email wont be getting any email and my.email blokey will be getting someone elses emails.

clever duh!

NogDog
01-13-2007, 08:41 PM
I would like to point out that strtolower(... which is wrapped around the email field data is a bad thing to do.

What about all those people who have upper case addresses?

My.Email@somewhere.com is not the same address as my.email@somewhere.com so blokey at My.Email wont be getting any email and my.email blokey will be getting someone elses emails.

clever duh!
Don't know about all mail servers, but mine does not care how things are captialized. I just tried sending myself two emails, one with everything upper-case and one with all lower-case. Both were received just fine.

OM2
01-15-2007, 08:10 PM
2 things:

1. i too have never heard about the case of email addresses mattering.

2. the address for my code is not for email addresses but rather physical addresses - so i do need the strlower function. are you saying there is something wrong with the code? if so... let me know.

thanks.

netbuddy
01-16-2007, 09:51 PM
Well that depends on the server configuration, I have tried out several pop servers and all of them allow upper and lower case and numeric in the address.

you can have...

My.Email
My.email
my.email
my.Email
MY.Email
my.EMAIL
and so on

for example and if your server allows those settings (some of the simpler servers dont) you can increase the number of boxes that your server holds.

For example, yahoo for years didnt allo the use of '.' in the free sign ups, since the shake up, I have an email address which has a . in it whch for years had a _ where a . used to be, eg My_Email and it's only in this last 12 months that the . is now used, I have an email address like this, my.email@domain.tld but they have from the outset allowed upper and lower case sensitivity in both passwords and email addresses.

NightShift58
01-16-2007, 10:30 PM
i too have never heard about the case of email addresses mattering.This was the subject of part of a thread recently with FM Bokeh posting a link to the RFC governing this issue. The rules were ambiguous at best and we more-or-less came to the conclusion that that was probably why capitalization was ignored by all mail servers with which we had had any experience.