Problems with string handling
Posted: Sat Nov 14, 2009 11:05 am
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("£","£",$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$*!()\'£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 £
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
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("£","£",$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$*!()\'£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 £
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