tricky /n

This is a discussion on tricky /n within the PHP Language forums, part of the PHP Programming Forums category; is /n counted as one character or two? ie I want to cut any line breaks from the begining of ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-25-2004
brendan
 
Posts: n/a
Default tricky /n

is /n counted as one character or two?
ie I want to cut any line breaks from the begining of a message
should it be
while(substr($message,0,1)=="\n") $message=substr($message,1);
or
while(substr($message,0,3)=="\n") $message=substr($message,2);

???
Reply With Quote
  #2 (permalink)  
Old 10-25-2004
Michael Fesser
 
Posts: n/a
Default Re: tricky /n

.oO(brendan)

>is /n counted as one character or two?


Depends.

>ie I want to cut any line breaks from the begining of a message
>should it be
>while(substr($message,0,1)=="\n") $message=substr($message,1);
>or
>while(substr($message,0,3)=="\n") $message=substr($message,2);


trim() exists.

Micha
Reply With Quote
  #3 (permalink)  
Old 10-25-2004
Andy Hassall
 
Posts: n/a
Default Re: tricky /n

On Mon, 25 Oct 2004 13:24:00 +0100, brendan
<brendan_nospam_@srl.cam.ac.uk_nospam> wrote:

>is /n counted as one character or two?


"/n" is two.
'/n' is two.
'\n' is two.
"\n" is ONE - a newline character.

>ie I want to cut any line breaks from the begining of a message
>should it be
>while(substr($message,0,1)=="\n") $message=substr($message,1);
>or
>while(substr($message,0,3)=="\n") $message=substr($message,2);


As a further complication, the end of line in Windows /is/ two characters -
"\r\n". Whereas on Unix, it's one, "\n". (I think on Macs it is just "\r").

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Reply With Quote
  #4 (permalink)  
Old 10-25-2004
Pedro Graca
 
Posts: n/a
Default Re: tricky /n

brendan wrote:
> is /n counted as one character or two?


two characters, no matter how you delimit it

"/n" == '/n'


However, with a backslash instead, it is different:

strlen("\n") == 1
strlen('\n') == 2



> ie I want to cut any line breaks from the begining of a message
> should it be
> while(substr($message,0,1)=="\n") $message=substr($message,1);
> or
> while(substr($message,0,3)=="\n") $message=substr($message,2);
>
> ???


Neither. use ltrim()

http://www.php.net/ltrim
--
USENET would be a better place if everybody read: | to mail me: simply |
http://www.catb.org/~esr/faqs/smart-questions.html | "reply" to this post, |
http://www.netmeister.org/news/learn2quote2.html | *NO* MIME, plain text |
http://www.expita.com/nomime.html | and *NO* attachments. |
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:19 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0