Date: 06/27/98
- Next message: Egon Schmid: "[PHP-DEV] Compile failure with current version"
- Previous message: owl: "[PHP-DEV] CVS update: php3"
- In reply to: Philip Quaife: "[PHP-DEV] RE: Bug #485 Updated: ereg_replace and Linefeeds"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
> I obviously was not clear on what I wanted and what I got.
>
> Even with your example, the resultant string was having the letter n
> on its own being replaced by new-line character.
No it doesn't. My example was:
$s1="One\nTwo\n\nEnd\nWhere have the nnns gone";
$s2 = ereg_replace("\n","\\\n",$s1);
echo $s2;
This displays:
One\
Two\
\
End\
Where have the nnns gone
This is what I thought you wanted, and the letter n on its own is not
replaced. (Try it)
> 1. replace the new-line with the two character sequence backslash+n
Ok, in this case you need:
$s1="One\nTwo\n\nEnd\nWhere have the nnns gone";
$s2 = ereg_replace("\n","\\n",$s1);
echo $s2;
This displays:
One\nTwo\n\nEnd\nWhere have the nnns gone
> So the error is that:
>
> Given a string that contains one backslash + one "n" character the
> command:
> ereg_replace("\\n","\n")
> should convert the two characters to a one character sequence
> (new-line).
No it shouldn't. Like I said, any quoted string in PHP understands escape
sequences. So when you write "\\n" this ends up being sent to the
ereg_replace() function as "\n" (which is not a newline).
> The Error is:
>
> That the backslash remains unconverted and that all letter "n" are
> replaced by the resultant string (new-line).
>
> ie. interpereted as: ereg_replace("n","\n")
Correct. This is the expected behaviour for what you wrote.
> I am not an expert on reqular expressions, but I believe that the
> expression should be evaluated left to right so that the term \\n
> should read as the two character sequence backslash+"n"
Correct, that is how it is being evaluated. The two-character sequence
backslash followed by an "n" is sent to the regex library. So what? You
are expecting the regex library to figure out that this sequence is
supposed to be a newline, or what are you trying to do? It is going to
look at this "\n" string and determine that it doesn't have a clue what
you are trying to do since it doesn't fit any of the special regex
escapes sequences and simply treat it like a normal "n".
I think we are probably misunderstanding each other somewhere along the
line. Perhaps it is because you don't realize that when you type:
$a = "One\nTwo";
Then in memory $a is actually stored as:
One
Two
ie. the \n is converted to a real new-line. When you perform a regex on
this, you need to look for a real new-line. However, if this same $a is
coming out of a database looking like this, then the \n is not turned into
a real newline. So, to simulate this directly it would be like having:
$a = "One\\nTwo";
Now, to replace the character "\" followed by "n" with something you need
to do it like this:
ereg_replace("[\\]n","something");
The []'s around the \\ ensure that the regex library doesn't try to do
anything further with the backslashes.
-Rasmus
- Next message: Egon Schmid: "[PHP-DEV] Compile failure with current version"
- Previous message: owl: "[PHP-DEV] CVS update: php3"
- In reply to: Philip Quaife: "[PHP-DEV] RE: Bug #485 Updated: ereg_replace and Linefeeds"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

