PHP中获取字符串的第一行

发布于 2025-01-01 00:51:09 字数 254 浏览 1 评论 0原文

在 PHP 5.3 中有一个 不错的函数 似乎可以做我想做的事情:

strstr(input,"\n",true)

不幸的是,服务器运行 PHP 5.2.17,可选的第三个参数 strstr 不可用。有没有办法在以前的版本中一行实现这一点?

In PHP 5.3 there is a nice function that seems to do what I want:

strstr(input,"\n",true)

Unfortunately, the server runs PHP 5.2.17 and the optional third parameter of strstr is not available. Is there a way to achieve this in previous versions in one line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

二手情话 2025-01-08 00:51:09

对于相对较短的文本,行可以由一个(“\n”)或两个(“\r\n”)字符分隔,单行可以

$line = preg_split('#\r?\n#', $input, 2)[0];

像第一个换行之前的任何序列一样,即使它是一个空字符串,

或者

$line = preg_split('#\r?\n#', ltrim($input), 2)[0];

第一个非空字符串。

但是,对于大文本,它可能会导致内存问题,因此在这种情况下,应该首选下面提到的 strtok 或其他答案中基于 substr 的解决方案。

当这个答案第一次写出来时,大约十年前,它有一些微妙的细微差别,

  • 它太本地化了,在开篇文章之后,假设行分隔符始终是单个“\n”字符,但情况并非总是如此。使用 PHP_EOL 不是解决方案,因为我们可以处理外部数据,不受本地系统设置的影响
  • ,假设我们需要第一个非空字符串,
  • 但没有办法使用 explode()preg_split() 在一行中,因此提出了一种使用 strtok() 的技巧。然而,不久之后,由于 统一变量语法,由 Nikita Popov,可以在简洁的单行中使用这些函数之一,

但随着这个问题越来越受欢迎,它是最好涵盖答案中所有可能的边缘情况。但由于历史原因,这里是最初的解决方案:

$str = strtok($input, "\n");

它将返回 unix 格式文本中的第一个非空行。

但是,考虑到行分隔符可能不同,并且 strtok() 的行为并不那么直接,因为“字符串开头或结尾的分隔符被忽略”,正如 C 中原始 strtok() 函数的手册页所述,现在我建议谨慎使用此函数。

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 the large texts it could cause memory issues, so in this case strtok mentioned below or a substr-based solution featured in the other answers should be preferred.

When this answer was first written, almost a decade ago, it featured a few subtle nuances

  • it was too localized, following the Opening Post with the assumption that the line delimiter is always a single "\n" character, which is not always the case. Using PHP_EOL is not the solution as we can be dealing with outside data, not affected by the local system settings
  • it was assumed that we need the first non-empty string
  • there was no way to use either explode() or preg_split() in one line, hence a trick with strtok() was proposed. However, shortly after, thanks to the Uniform Variable Syntax, proposed by Nikita Popov, it become possible to use one of these functions in a neat one-liner

but as this question gained some popularity, it's better to cover all the possible edge cases in the answer. But for the historical reasons here is the original solution:

$str = strtok($input, "\n");

that will return the first non-empty line from the text in the unix format.

However, given that the line delimiters could be different and the behavior of strtok() is not that straight, as "Delimiter characters at the start or end of the string are ignored", as it says the man page for the original strtok() function in C, now I would advise to use this function with caution.

还在原地等你 2025-01-08 00:51:09

虽然已经晚了,但你可以使用爆炸。

<?php
$lines=explode("\n", $string);
echo $lines['0'];
?>

It's late but you could use explode.

<?php
$lines=explode("\n", $string);
echo $lines['0'];
?>
画中仙 2025-01-08 00:51:09
$first_line = substr($fulltext, 0, strpos($fulltext, "\n"));

或者有关的东西可以解决这个问题。丑陋,但可行。

$first_line = substr($fulltext, 0, strpos($fulltext, "\n"));

or something thereabouts would do the trick. Ugly, but workable.

花落人断肠 2025-01-08 00:51:09

尝试

substr( input, 0, strpos( input, "\n" ) )

try

substr( input, 0, strpos( input, "\n" ) )
音盲 2025-01-08 00:51:09

echo str_replace(strstr($input, '\n'),'',$input);

echo str_replace(strstr($input, '\n'),'',$input);

So要识趣 2025-01-08 00:51:09
list($line_1, $remaining) = explode("\n", $input, 2);

如果您想重复操作,可以轻松获取顶行和留下的内容。否则按照建议使用 substr 。

list($line_1, $remaining) = explode("\n", $input, 2);

Makes it easy to get the top line and the content left behind if you wanted to repeat the operation. Otherwise use substr as suggested.

瑕疵 2025-01-08 00:51:09

不依赖于换行符号的类型。

(($pos=strpos($text,"\n"))!==false) || ($pos=strpos($text,"\r"));

$firstline = substr($text,0,(int)$pos);

如果没有找到中断符号(或者中断符号是文本中的第一个符号),$firstline 现在包含文本或空字符串的第一行。

not dependent from type of linebreak symbol.

(($pos=strpos($text,"\n"))!==false) || ($pos=strpos($text,"\r"));

$firstline = substr($text,0,(int)$pos);

$firstline now contain first line from text or empty string, if no break symbols found (or break symbol is a first symbol in text).

断桥再见 2025-01-08 00:51:09

试试这个:

substr($text, 0, strpos($text, chr(10)))

try this:

substr($text, 0, strpos($text, chr(10)))
甜扑 2025-01-08 00:51:09

您可以将 strpossubstr 结合使用。首先找到字符所在的位置,然后返回字符串的该部分。

$pos = strpos(input, "\n");

if ($pos !== false) {
echo substr($input, 0, $pos);
} else {
echo 'String not found';
}

这是你想要的吗?


没有注意到一行限制,所以这不适用。您可以按照其他人的建议将这两个函数组合在一行中,也可以根据需要创建一个将在一行代码中调用的自定义函数。你的选择。

You can use strpos combined with substr. First you find the position where the character is located and then you return that part of the string.

$pos = strpos(input, "\n");

if ($pos !== false) {
echo substr($input, 0, $pos);
} else {
echo 'String not found';
}

Is this what you want ?

l.e.
Didn't notice the one line restriction, so this is not applicable the way it is. You can combine the two functions in just one line as others suggested or you can create a custom function that will be called in one line of code, as wanted. Your choice.

北方的巷 2025-01-08 00:51:09

很多时候,字符串操作会遇到以空行开头的变量,所以不要忘记评估您是否真的想在字符串的开头和结尾考虑白行,或者修剪它。另外,为了避免操作系统错误,请使用 PHP_EOL 用于以跨平台兼容的方式查找换行符(什么时候使用 PHP 常量“PHP_EOL”?)。

$lines = explode(PHP_EOL, trim($string));
echo $lines[0];

Many times string manipulation will face vars that start with a blank line, so don't forget to evaluate if you really want consider white lines at first and end of string, or trim it. Also, to avoid OS mistakes, use PHP_EOL used to find the newline character in a cross-platform-compatible way (When do I use the PHP constant "PHP_EOL"?).

$lines = explode(PHP_EOL, trim($string));
echo $lines[0];
半岛未凉 2025-01-08 00:51:09

一种快速获取字符串前 n 行的方法,作为字符串,同时保留换行符。

示例 6 $multilinetxt 的第一行

echo join("\n",array_splice(explode("\n", $multilinetxt),0,6));

可以快速调整以捕获特定的文本块,例如第 10 行到第 13 行:

echo join("\n",array_splice(explode("\n", $multilinetxt),9,12)); 

A quick way to get first n lines of a string, as a string, while keeping the line breaks.

Example 6 first lines of $multilinetxt

echo join("\n",array_splice(explode("\n", $multilinetxt),0,6));

Can be quickly adapted to catch a particular block of text, example from line 10 to 13:

echo join("\n",array_splice(explode("\n", $multilinetxt),9,12)); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文