|
-
Jun 22nd, 2006, 09:28 AM
#1
Thread Starter
Frenzied Member
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
-
Jun 22nd, 2006, 09:34 AM
#2
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
-
Jun 22nd, 2006, 09:48 AM
#3
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");
-
Jun 22nd, 2006, 09:53 AM
#4
Re: Number of lines in a string
 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
 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
-
Jun 22nd, 2006, 09:56 AM
#5
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.
-
Jun 22nd, 2006, 10:46 AM
#6
Re: Number of lines in a string
If it is a windows file you will need to use the DOS CRLF sequence - "\r\n"
-
Jun 22nd, 2006, 10:47 AM
#7
Re: Number of lines in a string
The number of \n's will always match the \r's in that case.
-
Jun 22nd, 2006, 03:53 PM
#8
Re: Number of lines in a string
 Originally Posted by penagate
The number of \n's will always match the \r's in that case.
Indeed 
/pretends to understand
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|