explode()不能用空格分隔字符串吗? [英] explode() doesn't separate a string by spaces?

查看:135
本文介绍了explode()不能用空格分隔字符串吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图打开一个文本文件,并将其中的名称列表分解为一个数组,但是当我 var_dump 新数组时,我得到了:



array(1){[0] => string(61) name name name name}



列表的全部内容进入数组中的一个键字段。 / p>

这是我正在使用的代码:

  $ ingame_list = file_get_contents('../../../../ home / folder / folder / list.txt'); 
$ newarray = explode(,$ ingame_list);
var_dump($ newarray);

如何使每个名称都在新数组中位于其自己的键字段中?

解决方案

tl; dr 您的列表用简单空格以外的其他内容分隔-最有可能是多个相邻的空白字符。您实际上正在看到数据的HTML呈现,其中多个相邻的空白字符显示为一个。



请注意字符串(61) var_dump()的结果中。 var_dump()报告变量的类型,长度和内容(对于字符串)。 string(61)表示您的字符串中有61个字符。因此,如果您真的要返回字符串 name name name name ,则表明有些字符在这里看不到。这可能是因为空白在HTML中的显示方式:制表符,换行符和其他空白字符都看起来像简单的空格。您确定文本由空格分隔,而不是由制表符或其他空白分隔吗?



explode()仅会分隔与指定字符/字符串完全匹配的字符串。如果文本用 \t \n \r 或其他非打印字符,则您的 explode()语句将不起作用。例如,如果您有这样的话:

  name name name 

(假设这些是上面的制表符),则您的 explode()找不到任何普通的旧空格( ,字符值32)个字符。



如何解决此问题



根据下面的评论,您的数据包含换行符( \n ),而不是空格。您需要使用以下代码替换代码的第二行:

  $ newarray = explode( \n,$ ingame_list ); 


I am attempting to open a text file and explode the list of names within into an array but when I var_dump the new array I get this:

array(1) { [0]=> string(61) "name name name name " }

The entire contents of the list goes into one single key field in the array.

This is the code I'm using:

$ingame_list = file_get_contents('../../../../home/folder/folder/list.txt');
$newarray = explode(" ", $ingame_list);
var_dump($newarray);

How can I get each name to be in its own key field within the new array?

解决方案

tl;dr Your list is separated by something other than simple spaces - most likely multiple adjacent whitespace characters. You're actually seeing an HTML rendering of the data, in which multiple adjacent whitespace characters are displayed as one.

Note the string(61) in the results of var_dump(). var_dump() reports the type, length, and content of a variable (in the case of a string). That string(61) means there are 61 characters in your string. So, if you are really getting back the string "name name name name ", this implies there are characters that you are not seeing here. This could be because of the way whitespace displays in HTML: tabs, newlines, and other whitespace characters all look like simple spaces. Are you sure the text is separated by spaces, not tabs or other whitespace?

explode() will only split the string on exact matches to the specified character/string. If the text is separated by \t, \n, \r, or other non-printing characters, your explode() statement won't work. For example, if you have this:

name    name    name

(pretend those are tab characters above), then your explode() doesn't find any plain old space (, character value 32) characters.

How to Fix This

Based on your comments below, your data contains newline (\n) characters, not plain spaces. You need to replace the second line of your code with this:

$newarray = explode("\n", $ingame_list);

这篇关于explode()不能用空格分隔字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆