简体   繁体   English

PHP-get_file_contents无法用作数组?

[英]PHP - get_file_contents not working as an array?

I'm using a $file_contents = file_get_contents($file_name) then using $file_contents = array_splice($file_contents, 30, 7, 'changedText') to update something in the file code. 我正在使用$file_contents = file_get_contents($file_name)然后使用$file_contents = array_splice($file_contents, 30, 7, 'changedText')更新文件代码中的某些内容。 However, this keeps resulting in: 但是,这一直导致:

Warning: array_splice(): The first argument should be an array

From what I understand the string returned by file_get_contents() should be able to be acted on like any other array. 据我了解,file_get_contents()返回的字符串应该能够像其他数组一样作用。 Any reason I'm having trouble with this? 我有什么麻烦吗? Thank you much! 非常感谢!

From the manual : 手册

file_get_contents — Reads entire file into a string file_get_contents —将整个文件读入字符串

So you don't have an array. 所以你没有数组。 You have a string. 你有一个字符串。

Read documentation . 阅读文档

String is not an array even when it supports using square brackets: 即使字符串支持使用方括号,它也不是数组:

$str[0]

Use str_split function for behavior you want. 使用str_split函数实现所需的行为。 It will convert your string into real array, and then you can use it as an argument in array_splice function. 它将字符串转换为实数数组,然后可以将其用作array_splice函数中的参数。 Eg: 例如:

echo('<pre>');
var_dump(array_slice(str_split("Stack Overflow"), 6));
echo('</pre>');
die();

I think it helps. 我认为这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM