I have a string...

I want to replace all of the M chacters with \n characters. This string came from fgetc() 'ing a binary file until the feof().

I've tried:

preg_replace("M", "\n", $line);

preg_replace("^M", "\n", $line);

preg_replace("/^M/", "\n", $line);

The only one that doesn't complain is the third one, but it doesn't work, it just fails...

Any ideas?

    I cant remember offhand what's with the M, but you might want to take a look at the file using a hex editor.

    Basically, you may need to replace \n with \r\n, or vice versa, or even involve \r (though I doubt that).

      Originally posted by laserlight
      I cant remember offhand what's with the M, but you might want to take a look at the file using a hex editor.

      Basically, you may need to replace \n with \r\n, or vice versa, or even involve \r (though I doubt that).

      Hmm...not sure what you're getting at with the /r/n stuff. My file doesn't have those characters, it as M which, as I assume we all know, is a carriage return.

      The only function that even acknowledges these characters is ctype_cntrl(). I can sucessfully use it to insert a \n whenever it returns true (I'm reading the file char by char). This will work, but
      I'd rather just run a preg_replace on the whole string once it's read in. But I can't find a single function other than the ctype_cntrl() function that 'sees' the M character. I don't get it.

      EDIT: OK, I looked at the file with a Hex editor and apparently it's hex characters 0D and 01 that I want to strip out and replace with a \n character. I don't know how to use this information in a preg_replace() call tho...can anyone guide me?

      Thanks!

      Jon

        My file doesn't have those characters, it as M which, as I assume we all know, is a carriage return.

        If M is a carriage return (i.e. 0D), then it can also be represented as \r
        In this case, you're just looking at a control character (sequence) being spit out at you.

        apparently it's hex characters 0D and 01 that I want to strip out and replace with a \n character

        I was expecting 0D 0A (i.e. \r\n, the MS Windows newline sequence) actually, but it doesnt really matter that much, I suppose.

        Anyway, you can still use the regex \r\x01 to match 0D0A (or \r\x01, in the event \r gets parsed by PHP and you dont use the /m modifier (if that does help with \r, I'm not too clear).

          Originally posted by jonwatson
          I've tried:

          preg_replace("M", "\n", $line);

          preg_replace("^M", "\n", $line);

          preg_replace("/^M/", "\n", $line);

          But have you tried [man]str_replace[/man]? Note that (as has already be mentioned) that "M" won't match carriage return characters; you'd need to use either the conventional C escape sequence or the explicit ASCII code. If you're replacing single characters by single characters, consider [man]strtr[/man].

            If you're replacing single characters by single characters

            He isnt, from the looks of it.

            I'm still puzzled as to why 0x01 would be lying around in the data.

              Originally posted by laserlight
              I'm still puzzled as to why 0x01 would be lying around in the data.

              I think maybe it's a typo, but since this is a binary file - where any byte could happily turn up, I'm figuring the exact value doesn't signify when it comes to knowing how to convert it - whaterver it is. Like you say, it doesn't really matter that much.

                a year later

                perhaps it was x010 instead of x01 ... 10 hex is 0A ( line feed ) so x0D x010 could be a typo of x0D x0A

                Just a thought.

                maybe:
                $replacethis = chr(13).chr(10);
                $withthis = chr(10);
                $fixedline = preg_replace($replacethis,$withthis,$line);

                  yeah M is usually a carriage return... it just shows up in some text editing programs like that (I think vim might do this sometimes).

                  You can just do something like this to "clean up" all new line feeds (whether \n or \r\n or \r) and change them to just \n:

                  preg_replace('/\r\n?/', "\n", $text);

                    wait a minute... what the hell, I thought this was a new thread. Who dug up a 5 month old already answered thread? lol

                      You mean, a 1 year and 5 month old thread ;]

                        Write a Reply...