Obtain first line of a string in PHP
For the relatively short texts, where lines could be delimited by either one (“\n”) or two (“\r\n”) characters, the one-liner could be like $line = preg_split(‘#\r?\n#’, $input, 2)[0]; for any sequence before the first line feed, even if it an empty string, or $line = preg_split(‘#\r?\n#’, ltrim($input), 2)[0]; for the first non-empty string. However, for … Read more