Question

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. 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. Any reason I'm having trouble with this? Thank you much!

Was it helpful?

Solution

From the manual:

file_get_contents — Reads entire file into a string

So you don't have an array. You have a string.

OTHER TIPS

Read documentation.

String is not an array even when it supports using square brackets:

$str[0]

Use str_split function for behavior you want. It will convert your string into real array, and then you can use it as an argument in array_splice function. E.g:

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

I think it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top