Problems with string handling

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Bob Kellock
Forum Newbie
Posts: 10
Joined: Fri Oct 30, 2009 10:57 am
Location: Wiltshire, UK

Problems with string handling

Post by Bob Kellock »

I'm having great difficulty in understanding how strings are processed in PHP. I have an input field in a form which accepts any normal character and spaces.
In the page which is actioned by the form I want to exactly (1) display the string entered in the form and (2) to include in it an email that I am sending.
So far neither is exactly right.

Fragments of source follow:-

Both pages include, in the head,
<META http-equiv=content-type content="text/html; charset="UTF-8">

My first page includes:
<form enctype="multipart/form-data" id="form1" name="form1" action="http://xxx.co.uk/page2.php" method="post" accept-charset="UTF-8">
<input type="text" name="yourname" size="50" value=""> <!-- REF 1 -->
<input type="submit" name="submit" value="Continue to next stage" >
</form>

page2.php (which uses Session variables as they may also be required in later
pages) includes :-

<?php
$_SESSION['$SVname'] = $_REQUEST["yourname"];
$name = $_SESSION['$SVname'];
?>
..........
<?php echo $name; ?> // REF 2
..........
<?php
$sender = "a@b.com";
$name = str_replace("£","&pound;",$name); // REF 3 - also tried with this line commented out
mail("me@outwiththefairies.co.uk", "Customer details", $name, "From: $sender");
?>

RESULTS
Please note that I have substituted + for each space because when I preview this message before posting it I see that the forum software is stripping out multiple spaces!
Is that a bug in the forum's software?

If enter at the form Bill+++++Bloggs$*!()£Ted (REF 1 line)
then at REF 2 I get Bill+Bloggs$*!()£Ted
i.e. the £ is correct but the multiple spaces have been replaced by a single one.

With the REF 3 line not commented out the email message is
Bill+++++Bloggs$*!()\'&pound;Ted
but, if I comment out the line at REF 3, the email message is
Bill+++++Bloggs$*!()\'Ted
i.e. the multiple spaces are correct but the £ is not included or is changed to &pound;

I have tried it with the £ character in other positions and the behaviour has
been the same. I haven't Escaped any of the characters such as $.

Bob
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problems with string handling

Post by requinix »

Seeing one space instead of multiple spaces is just an illusion. Just because you can't see them doesn't mean they aren't there.

Regarding the pound sign I think you have something mistaken. Characters do not appear and disappear like that.
Bob Kellock
Forum Newbie
Posts: 10
Joined: Fri Oct 30, 2009 10:57 am
Location: Wiltshire, UK

Re: Problems with string handling

Post by Bob Kellock »

I think that it's only illusory because the browser renders multiple spaces as a single space.
If the additional spaces are converted to nbsp; in the php then it displays correctly. I've looked for a php function that does that but couldn't find one and, with my inexperience, it will take quite a long time to write one that works properly! Has anybody got a ready made one?

I'm 100% sure that I'm not mistaken about the £ character, there's definitely something weird about it.
I noticed that using UTF-8 it was not displayed correctly elsewhere from the code snippets that I posted previously. Windows-1250 displayed the pound sign but preceded by an A with a superimposed caret (Unicode 00C2 I think). So far, I haven't noticed any irregularities when using ISO-8859-1.

Presumably php's handling of strings in the mail function is quite different to that for echo.
How do I beat it into submission?

Bob
Bob Kellock
Forum Newbie
Posts: 10
Joined: Fri Oct 30, 2009 10:57 am
Location: Wiltshire, UK

Re: Problems with string handling

Post by Bob Kellock »

tasairis wrote: ..... Characters do not appear and disappear like that.
Oh yes they do! but in a somewhat unexpected manner.
When I looked at the raw version of the received email the £ was there so it seems that my email client, Pegasus Mail, doesn't treat a £ character as such.
When I sent a mail from Pegasus any pound character in the composed message was replaced by =A3 in the transmitted version.
I guess that I'll have to delve into the dreaded RFCs about email to find out what else needs special treatment.

As far as the missing spaces I now realise that it's a simple call to str_replace.

Bob
P.S. Why does the spell checker not realise that realise is a correct alternative to realize? :lol:
Post Reply