How to get the first line of a multi-line string in PHP

2 Answers

0 votes
$multiLineString = 
    "First line\n" .
    "Second line\n" .
    "Third line\n" .
    "Fourth line";

$firstLine = substr($multiLineString, 0, strpos($multiLineString, "\n"));

echo "The first line is: " . $firstLine;



/*
run:

The first line is: First line

*/

 



answered Aug 13, 2024 by avibootz
0 votes
$multiLineString = 
    "First line\n" .
    "Second line\n" .
    "Third line\n" .
    "Fourth line";

$firstLine = strtok($multiLineString, "\n");

echo "The first line is: " . $firstLine;



/*
run:

The first line is: First line

*/

 



answered Aug 13, 2024 by avibootz

Related questions

3 answers 260 views
260 views asked Oct 12, 2018 by avibootz
1 answer 115 views
1 answer 110 views
1 answer 109 views
1 answer 98 views
1 answer 105 views
1 answer 98 views
...