0

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!

2 Answers 2

6

From the manual:

file_get_contents — Reads entire file into a string

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

Sign up to request clarification or add additional context in comments.

3 Comments

file() function reads file into an array of lines. I think question is about accessing string characters via squared brackets, which looks like an array, but in fact it's not - it's just a PHP helpful syntax.
Yeah - that's why I added answer as well. But you were faster :)
Ah, then my mistake was thinking a string was an array. For some reason that was my understanding.
1

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.

2 Comments

Thanks, my confusion was in assuming a string was a form of array in PHP.
That's why str_split can help you in converting string to an array;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.