0

This is really bizarre because I checked that the two strings which I have concatenate does not have a space between them in the HTML source code. However, when PHP parse the text in my update.php file, it returns string1 string2 instead of string1string2 which is the expected output.

What went wrong?

Update.php

<?php

    $string1 = $_POST['string1'];
    $string2 = $_POST['string2'];
    $filename = str_replace(" ", "", substr($string1, 14))
        . str_replace(" ", "", substr($string2, 11));


    echo $filename;
?>

$_POST['string1'] = Exchange Name: Osaka;
$_POST['string2'] = Stock Name: 20; 
5
  • 1
    what are the values for $_POST["string1"] and 2 Commented Dec 31, 2012 at 6:11
  • Are you sure the space is a space, and not a tab or newline character? Commented Dec 31, 2012 at 6:12
  • @HankyPanky: Added the values. Commented Dec 31, 2012 at 6:16
  • @Michael: How may I check whether the space is a space and not a tab or newline character? please advise. Commented Dec 31, 2012 at 6:17
  • 1
    @TingPing My guess is that you're checking the output in a browser. What you want to do is to view the source code of the output so it's not html-formatted. Pass an array of all possible character to str_replace instead of just space, e.g. str_replace(array(' ', '\n', '\t'), '', $subject); Commented Dec 31, 2012 at 6:20

4 Answers 4

3
$string1 = "Exchange Name: Osaka";
$string2 = "Stock Name: 20";
$pattern = '/[\W]/i'; // you can add extra conditions to handle other chars as well
$replacement = "";

echo preg_replace($pattern, $replacement, $string1)
    . preg_replace($pattern, $replacement, $string2);
Sign up to request clarification or add additional context in comments.

2 Comments

Warning: preg_replace() [function.preg-replace]: Unknown modifier '\' in D:\wamp\www\update.php on line 11
Remove the slash in the middle. Should be $pattern = '/\n\t/i';
2

Possibly try using trim instead of replace.

    trim(substr($string1,14));

Hope that helps.

1 Comment

trim would remove spaces only from ends, not if they are in any other position
1

After populating filename, You can use

$filename = str_replace(" ", "", $filename);

But this is not a proper solution considering we didnt yet figure out where the issue lies, it is just a fix. So first find the issue so you know why it is happening

Then somehow you are getting new line characters from the form which posted these values. Try this

$filename = str_replace("\r", "", $filename);
$filename = str_replace("\n", "", $filename);

5 Comments

I just ran your code and it seems to work as you expect it to. You sure its showing a space for you?
I am running on windows. I am not sure whether that will affect the result?
view source of the output, see if shows with a space or a newline? for me your code outputs Osaka20
The source output shows that the two strings are separated by a new line. Hmm, seems like the 'space' problem is really a newline issue. How may i get rid of that new line?
check update. you can consolidate those 2 replaces in one and just replace "\r\n"
0

I solved it by using the following:

echo str_replace(PHP_EOL, '', $filename);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.