将硬引号添加到列表字符串
假设我有一个包含字符串列表的文本文件
,如下所示:
434242019884
434244064888
434240746884
434241083881
使用 PHP 回显用硬引号 (') 括起来的最有效方法是什么?
我只是好奇。
Lets say I have a text file with a list of strings
like so:
434242019884
434244064888
434240746884
434241083881
Using PHP what is the most efficient way to echo them back wrapped with hard quotes (')?
I'm just curious.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这将正确打印您的数据,并考虑数组创建的换行符。
获取文件内容。如果文件中有多于一行文本,它将创建一个数组,其中每一行作为一个项目。
启动一个循环,将每个数组项放入一个变量中。
从字符串末尾修剪新行。 ( \x0A = \n )
回显字符串,在我们想要的位置添加换行符。
结束循环。
<前><代码>}
一下子就全部完成了:
This will print your data properly, taking the newlines created by arrays into account.
Get the file contents. If there is more than one line of text in the file, it will create an array with each line as an item.
Start a loop that puts each array item in a variable.
Trim the new line from the end of the string. ( \x0A = \n )
Echo the string, adding the newline where we want it.
End the loop.
Here it is all at once:
怎么样:
编辑:我根据您预先编辑的消息中显示的空格编写了代码..但是您可以将 str_replace 中的空格更改为 EOL
编辑2: $string 实际上是整个字符串列表,顺便说一句
how about:
EDIT: i did the code based on the spaces shown on your pre-edited message.. but you can change the space in the str_replace to an EOL
EDIT2: the $string is actually the whole list of strings, btw
这是在单行中使用爆炸/内爆函数执行此操作的方法:
如果文件行被“\n”(Linux 风格)而不是“\r\n”(Windows 风格)划分,则此方法将不起作用,只需修复将“\r\n”爆炸参数更改为“\n”即可。
通过这种方式,您可以控制第一个和最后一个硬引号(如果您不需要最后一个
),并中继到库函数。
我只是不知道与其他解决方案相比,这有多快。
编辑:做了一些测试,表现完美(也许也比其他解决方案更快)。
This is how you do it using the explode/implode function in a single row:
This could not work in case your file lines are divided by "\n" (Linux style) instead of "\r\n" (Windows style), just fix changing the "\r\n" explode parameter with "\n".
This way you can control the first and the last hardquotes (in case you don't want the last <br/>) and you relay to library functions.
I just don't know how fast is this compared with other solutions.
Edit: made some tests, performs perfectly (maybe also faster than other solutions).