Results 1 to 8 of 8

Thread: Number of lines in a string

  1. #1

    Thread Starter
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Number of lines in a string

    Need to know how many lines in a string.

    "frnejkgv]

    rfgdrf
    fdgfd

    fdgdf" = 6

    "vfv

    fdb
    bfd" = 4

    Thanks

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Number of lines in a string

    PHP Code:
    $line split("\\n"$input_several_lines_long);

    echo 
    count($line

    HTH

    P.S. - Just so you know, I don't really know PHP, but when you ask questions, I look under http://www.php.net , and tend to find answers pretty quickly.

    Might be a little quicker for you than posting on here

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Number of lines in a string

    \\n won't work, that will give you backslash n not a newline character.

    PHP Code:
    $lineCount substr_count($string"\n"); 

  4. #4
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Number of lines in a string

    Quote Originally Posted by penagate
    \\n won't work, that will give you backslash n not a newline character.

    PHP Code:
    $lineCount substr_count($string"\n"); 

    Hmm, on the page I was reading, someone posted that it requires \\n, not \n...

    Ill see if I can find the post..Maybe there was a certain circumstance for it


    *edit* Found the post, looks like its supposed to be /\n instead of \\n

    Quote Originally Posted by http://ca3.php.net/manual/en/function.split.php#49130
    A correction to a earlier note
    If you want to use split to check on line feeds (\n), the following won't work:

    $line = split("\n", $input_several_lines_long);

    You really have to do this instead, notice the second slash:
    $line = split("/\n", $input_several_lines_long);

    Took me a little while to figure to do

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Number of lines in a string

    FYI, if you use single quotes the escape sequences are turned off. It's always better to use single quoted string literals if you can as the overheads of parsing the string for variables/escape sequences are removed.


    Edit: I really have no idea why you'd want to use /\n. It makes no sense.

  6. #6
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Number of lines in a string

    If it is a windows file you will need to use the DOS CRLF sequence - "\r\n"
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Number of lines in a string

    The number of \n's will always match the \r's in that case.

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Number of lines in a string

    Quote Originally Posted by penagate
    The number of \n's will always match the \r's in that case.
    Indeed

    /pretends to understand
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width