Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 11-17-2004, 09:13 PM   #1 (permalink)
Admin
$_['Your_Mom'];
 
Admin's Avatar
 
Join Date: May 2002
Location: Santee
Posts: 627
Admin is on a distinguished road
removing ^M characters?

anyone know how to remove those annoying '^M' characters that microsoft adds at the end of a line? str_replace isn't working because i can't get the character to reproduct properly.

any ideas?!?
__________________


Urban Clothing
Admin is offline   Reply With Quote
Old 11-17-2004, 09:31 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,487
sde is on a distinguished road
i hate that crap and i don't know.
__________________
Mike
sde is offline   Reply With Quote
Old 11-17-2004, 09:33 PM   #3 (permalink)
Admin
$_['Your_Mom'];
 
Admin's Avatar
 
Join Date: May 2002
Location: Santee
Posts: 627
Admin is on a distinguished road
the only idea i have right now is to split my string into an array and just chop off the last character from each line. nasty.

you about ready for a snow trip?
__________________


Urban Clothing
Admin is offline   Reply With Quote
Old 11-17-2004, 09:56 PM   #4 (permalink)
Admin
$_['Your_Mom'];
 
Admin's Avatar
 
Join Date: May 2002
Location: Santee
Posts: 627
Admin is on a distinguished road
this seems to be working...

PHP Code:
    $OutPut explode("\n",$_POST[info]);

    for(
$i=0$i count($OutPut); $i++)
    {
      
$RealOutPut .= substr($OutPut[$i], 0, -1) . "\n";
    } 
__________________


Urban Clothing
Admin is offline   Reply With Quote
Old 11-17-2004, 10:20 PM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,487
sde is on a distinguished road
yeah, i'm ready for a trip. i have the AWD, but i just put performance street tires on lol .. it will be interesting.
__________________
Mike
sde is offline   Reply With Quote
Old 11-18-2004, 05:08 AM   #6 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
PHP Code:
$str "nice line with crap on the end\r\n";
$str preg_replace("/[\r\n]/"""$str); 
Or just "/[\r]/" to remove the CR and no the unix LF.

-r
idx is offline   Reply With Quote
Old 11-18-2004, 05:10 AM   #7 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Not php, but useful:

perl command line one-liner:

Code:
perl -pi.orig -e 's/\r//g;' file
(removes all CR's from the file and saves the original file as "file.orig")

perl:
Code:
while (<>) {
  chomp;
  s/\r//g;
  # do stuff
}
..or remove the chomp and just use s/[\r\n]//g;

-r
idx is offline   Reply With Quote
Old 11-18-2004, 05:17 AM   #8 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
PHP Code:
$str "ms cruft\r\n";
$str str_replace ("\r\n"""$str); 
This might work too..
idx is offline   Reply With Quote
Old 11-18-2004, 06:43 AM   #9 (permalink)
Admin
$_['Your_Mom'];
 
Admin's Avatar
 
Join Date: May 2002
Location: Santee
Posts: 627
Admin is on a distinguished road
idx, you think that will match the ^M character? I didn't even bother trying that because VIM was displaying ^M at the end of every line..
__________________


Urban Clothing
Admin is offline   Reply With Quote
Old 11-18-2004, 03:48 PM   #10 (permalink)
DavH27
PHP Pilgrim
 
DavH27's Avatar
 
Join Date: Aug 2004
Location: London
Posts: 170
DavH27 is on a distinguished road
What is this ^M character all about? What do you mean 'Microsoft adds to the end of every line' ?

I'm confused now but if this happens to me in the future then I got some clue of why.
__________________
Davy - Programming since 1998 [CV]
Currently working on: n/a
Status: n/a
DavH27 is offline   Reply With Quote
Old 11-19-2004, 04:07 AM   #11 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Yeah, \n is the unix line feed and \r is the carriage return. Although in editors you see ^M. You can remove the ^M's in VIM too.

Press : to get in command mode. then type:

g/\r//s//g

(I think that's right. it's close)



http://us2.php.net/language.types.string

Check out table 11-1.


Quote:
What do you mean 'Microsoft adds to the end of every line' ?
Yes, by default MS ends every line in a plain text file with a carriage return then a line feed. (\r\n) But unix/linux/etc only use the line feed so we end up seeing the CR as if it was actually part of the file.

Ever try to open a unix file in notepad? It gets all squished together with little blocks (the LF's) in between since it can't find the CR to separate lines.

I believe MAC's did something weird like end lines with only a CR.. Although maybe that was before the OSX days.

-r
idx is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multi Byte Characters with PHP/MySQL sde PHP 1 03-21-2004 01:56 PM
How can I count characters in a text field? jimmyoctane PHP 2 10-18-2003 03:38 PM
array of characters in a structure TheDreamless Standard C, C++ 3 04-18-2003 04:30 PM


All times are GMT -8. The time now is 04:51 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting