1

I'm taking input from textarea form and put it in the long line. Input is like this:

Country: France

City: Paris

Street: Champ

Then put it in the long line:

$line = $_POST['input'];

now, I need to replace spaces and new lines with <SP> and <BR>, so I do this:

$line = str_replace(" ","&ltSP&gt",$line);
$line = str_replace("\n","&ltBR&gt",$line);

but I get this:

Country:<SP>France <BR> <BR>City:<SP>Paris <BR> <BR>Street:<SP>Champ

Now, if insted of \n I tried this:

$line = str_replace("\r","&ltBR&gt",$line);

I get similar result. If I do them both i Get similar results which obviously has some spaces in it. So Then I do this:

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

but it stays the same.

My whole code is:

 $line = str_replace(" ","&ltSP&gt",$line);
 $line = str_replace("\n","&ltBR&gt",$line);
 $line = str_replace(" ","",$line);
 echo $line;

How to remove spaces in this case?

Edit: I did bin2hex and found out that the space is actually carriage return \r. So this is the solution:

$line = str_replace("\r","",$line);

Why did \r behave like space in this case?

cikatomo
  • 1,620
  • 2
  • 23
  • 32
  • 1
    Are you sure your string is one long line, or is it really multiline? You may also be dealing with special whitespace characters, like the non-breaking space. – BoltClock Dec 25 '11 at 19:42
  • try replacing ` ` with "". i.e. `$line = str_replace(" ","",$line);` – Virendra Dec 25 '11 at 19:42
  • 2
    Use a hexdump of your string to really find out with which characters you're dealing, see [How can I get a hex dump of a string in PHP?](http://stackoverflow.com/questions/1057572/how-can-i-get-a-hex-dump-of-a-string-in-php) – hakre Dec 25 '11 at 19:45
  • Try looking at the source, and make sure it's not showing ` ` instead of `' '`. If so, do a string replace on ` `. – GManz Dec 25 '11 at 19:47
  • 1
    Post your original `$line` before any replacing is done because it isn't `$line = "some long line"`. You may have the ` ` character in it which is a space but won't be replaced by replacing `" "`. – MrCode Dec 25 '11 at 19:48
  • Your code is correct. The problem is in the string. Can you post the actual string being parsed? – Abhi Beckert Dec 25 '11 at 19:50
  • with hexdump I found out which character it is. I edit the question to be more clear and post the answer since i cannot answer. The space was caused by \r. If someone can explain why \r was manifested as space that would be good. – cikatomo Dec 25 '11 at 23:16

1 Answers1

1

the comments will probably suffice but I thought I'd share this snippet, which given your example I had a punt at what you are trying to achieve.

<?php

// example string data
$str = "key1 value1 key2 value2 key3 value3";

// clean string 
$str = str_replace("&nbsp", " ",$str);
$str = str_replace("\n", " ", $str);
$str = str_replace("\r", " ", $str);

$arr = explode(" ", $str);
$out = "<ul>";
$cnt = 1;

foreach($arr as &$val) {
    if ($cnt++ % 2 == 0) {
        continue;
    } else {
        $out = $out . "<li>" . $val . ": " . current($arr) . "</li>";
    }
}

$out = $out . "</ul>";

echo $out;

// output source
// <ul><li>key1: value1</li><li>key2: value2</li><li>key3: value3</li></ul>

?> 
T I
  • 9,785
  • 4
  • 29
  • 51