Connecting Tech Pros Worldwide Forums | Help | Site Map

First and last line

hendry
Guest
 
Posts: n/a
#1: Aug 29 '05
I would like to grab the first and last line of a form's textarea with
PHP's PCRE functionality. Anyone have a recipe to do that? Or have a
better suggestion?


ZeldorBlat
Guest
 
Posts: n/a
#2: Aug 29 '05

re: First and last line


Assuming the lines are separated by \n, you can simply do:

$lines = explode("\n", $text);
$firstLine = $lines[0];
$lastLine = array_pop($lines);

Hero Wanders
Guest
 
Posts: n/a
#3: Aug 29 '05

re: First and last line


Hello!
[color=blue]
> Assuming the lines are separated by \n, you can simply do:
>
> $lines = explode("\n", $text);
> $firstLine = $lines[0];
> $lastLine = array_pop($lines);[/color]

Be careful with array_pop() here!
If $lines is used later perhaps the missing line is needed.
You should either use

$firstLine = $lines[0];
$lastLine = $lines[count($lines)-1];

or

$firstLine = array_shift($lines);
$lastLine = array_pop($lines);

Greetings,
Hero Wanders
John Dunlop
Guest
 
Posts: n/a
#4: Aug 29 '05

re: First and last line


ZeldorBlat wrote:
[color=blue]
> Assuming the lines are separated by \n,[/color]

The spec says that, for both the POST and GET methods, lines
are separated by CRLF pairs.

--
Jock
Closed Thread