PHP中获取字符串的第一行
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
对于相对较短的文本,行可以由一个(“\n”)或两个(“\r\n”)字符分隔,单行可以
像第一个换行之前的任何序列一样,即使它是一个空字符串,
或者
第一个非空字符串。
但是,对于大文本,它可能会导致内存问题,因此在这种情况下,应该首选下面提到的
strtok
或其他答案中基于substr
的解决方案。当这个答案第一次写出来时,大约十年前,它有一些微妙的细微差别,
PHP_EOL
不是解决方案,因为我们可以处理外部数据,不受本地系统设置的影响explode()
或preg_split()
在一行中,因此提出了一种使用strtok()
的技巧。然而,不久之后,由于 统一变量语法,由 Nikita Popov,可以在简洁的单行中使用这些函数之一,但随着这个问题越来越受欢迎,它是最好涵盖答案中所有可能的边缘情况。但由于历史原因,这里是最初的解决方案:
它将返回 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
for any sequence before the first line feed, even if it an empty string,
or
for the first non-empty string.
However, for the large texts it could cause memory issues, so in this case
strtok
mentioned below or asubstr
-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
PHP_EOL
is not the solution as we can be dealing with outside data, not affected by the local system settingsexplode()
orpreg_split()
in one line, hence a trick withstrtok()
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-linerbut 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:
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.虽然已经晚了,但你可以使用爆炸。
It's late but you could use explode.
或者有关的东西可以解决这个问题。丑陋,但可行。
or something thereabouts would do the trick. Ugly, but workable.
尝试
try
echo str_replace(strstr($input, '\n'),'',$input);
echo str_replace(strstr($input, '\n'),'',$input);
如果您想重复操作,可以轻松获取顶行和留下的内容。否则按照建议使用 substr 。
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.
不依赖于换行符号的类型。
如果没有找到中断符号(或者中断符号是文本中的第一个符号),$firstline 现在包含文本或空字符串的第一行。
not dependent from type of linebreak symbol.
$firstline now contain first line from text or empty string, if no break symbols found (or break symbol is a first symbol in text).
试试这个:
try this:
您可以将
strpos
与substr
结合使用。首先找到字符所在的位置,然后返回字符串的该部分。这是你想要的吗?
乐
没有注意到一行限制,所以这不适用。您可以按照其他人的建议将这两个函数组合在一行中,也可以根据需要创建一个将在一行代码中调用的自定义函数。你的选择。
You can use
strpos
combined withsubstr
. First you find the position where the character is located and then you return that part of the string.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.
很多时候,字符串操作会遇到以空行开头的变量,所以不要忘记评估您是否真的想在字符串的开头和结尾考虑白行,或者修剪它。另外,为了避免操作系统错误,请使用 PHP_EOL 用于以跨平台兼容的方式查找换行符(什么时候使用 PHP 常量“PHP_EOL”?)。
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"?).
一种快速获取字符串前 n 行的方法,作为字符串,同时保留换行符。
示例 6 $multilinetxt 的第一行
可以快速调整以捕获特定的文本块,例如第 10 行到第 13 行:
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
Can be quickly adapted to catch a particular block of text, example from line 10 to 13: