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!
From the manual:
file_get_contents — Reads entire file into a string
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. 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.
Related
It is simple thing but I cannot resolve it myself.
I am trying to extract part of a string using substr:
$Text=substr($FullText, 0, 6);
Where $FullText is a varaible containing a string.
I guess substr does not like variable and needs a string in quotas.
My question is how can I substitute variable in PHP expression with its value?
Thank you very much in advance!
$text=substr($fulltext, 0, 6);
it will work, Substr function receives string in double quotes or you can put php variable in it. Try to echo this, it will work as required
You posted this code fragment in a comment to the question:
$FullText=$ExchangePart->Item(5)->nodeValue;
//actual value of the variable is: "1.6598 6.479460 5.4545"
$Text=substr($FullText, 0, 6);
//$Text=$ExchangePart->Item(5)->nodeValue;
echo $Text;
It looks like you are handling XML here. The SimpleXMLElement class (the type of the objects created by the simplexml_*() functions) is a beast with many faces. If you try to:
echo($ExchangePart->Item(5)->nodeValue);
or
print_r($ExchangePart->Item(5)->nodeValue);
it is presented as a string but, in fact, it is also an SimpleXMLElement object.
The solution to the issue is very simple: you have to explicitly convert the value you want to a string:
$FullText = (string)$ExchangePart->Item(5)->nodeValue;
Now the value of $FullText is a string; you can safely pass it to any string processing function and it will work as described in the documentation.
I am putting the contents of an text file into an array via the file() command. When I try and search the array for a specific value it does not seem to return any value but when I look at the contents of the array the value I am searching for is there.
Code used for putting text into array:
$usernameFileHandle = fopen("passStuff/usernames.txt", "r+");
$usernameFileContent = file("passStuff/usernames.txt");
fclose($usernameFileHandle);
Code for searching the array
$inFileUsernameKey = array_search($username, $usernameFileContent);
Usernames.txt contains
Noah
Bob
Admin
And so does the $usernameFileContent Array. Why is array_search not working and is there a better way to do this. Please excuse my PHP noob-ness, thanks in advance.
Because file():
Returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached
To prove this try the following:
var_dump(array_search('Bob
', $usernameFileContent));
You could use array_map() and trim() to correct the behavior of file(). Or, alternatively, use file_get_contents() and explode().
To quote the docs:
Each element of the array corresponds to a line in the file, with the newline still attached.
That means that when you're doing the search, you're searching for "Noah" in an array that contains "Noah\n" - which doesn't match.
To fix this, you should run trim() on each element of your array before you do the search.
You can do that using array_map() like this:
$usernameFileContent = array_map($usernameFileContent, 'trim');
Note, too, that the file() function operates directly on the provided filename, and does not need a file handle. That means you to do not need to use fopen() or fclose() - You can remove those two lines entirely.
So your final code could look like this:
$usernameFileContent = array_map(file('passStuff/usernames.txt'), 'trim');
$inFileUsernameKey = array_search($username, $usernameFileContent);
I have the following string: a:2:{s:4:"user";b:1;s:6:"userid";s:2:"48";}
What I need to do is extract number 48 from it, in this case. This number could have any number of digits. How would I go about doing this?
It looks like you are facing a serialized strings. So, instead of trying to get that number using regular expression or any other string manipulation methods, try this:
$myVar = unserialize('a:2:{s:4:"user";b:1;s:6:"userid";s:2:"48";}');
$myNumber = $myVar['userid'];
Learn about PHP serialization here:
http://php.net/manual/en/function.serialize.php
http://php.net/manual/en/function.unserialize.php
What exactly are you trying to achieve? That string looks like a serialize()d one, so your best bet would be to unserialize() it
It looks like serialized string.
$data = unserialize('a:2:{s:4:"user";b:1;s:6:"userid";s:2:"48";}');
print_r($data['userid']);
Looks like that's a serialized associative array. You just need to use unserialize() to turn it back from a string into an array.
<?php
$arr = unserialize('a:2:{s:4:"user";b:1;s:6:"userid";s:2:"48";}');
echo $arr['userid'];
?>
The string I see is serialized array in PHP
To unserialize array do this
$obj = unserialize('a:2:{s:4:"user";b:1;s:6:"userid";s:2:"48";}');
echo $obj['userid'];
I have unserialized array then access array param by name
I'm looking for a quick and easy way to take the contents of a multidimensional array and convert to a nicely formatted string. The string would (preferably) be the same as what we get when we:
echo "<pre>" . print_r($_POST) . "</pre>";
I could reproduce that with a home-grown function, but am hoping there is a built-in function to handle this (one I haven't so far been able to locate on php.net).
Thanks for any tips/pointers.
print_r() accepts a second parameter that when == true returns the output as a string instead of sending to the buffer.
Just do $str = print_r($_POST, true);
I have file txt and in the file I have phone numbers.
I want to filter out that the duplicate numbers. How I could do it using PHP?
Each number is a new line /r/n
You could:
Parse the string into an array, via explode
Filter out the dups, via array_unique
$numbers = Array();
$numbers = file('mydata.txt');
$numbers = array_unique($numbers);
This should do:
$numbers = array_unique(file('phones.txt'));
print_r($numbers);
Used functions file() and array_unique().
Good luck!
Further explanation.
The file() will:
Returns the file in an array. Each
element of the array corresponds to a
line in the file...
So you can use to your advantage that the phones are one on each line.
Note:
Just in case I may clarify that this won't work if the .txt file actually has /r/n
123/r/n
456/r/n
123/r/n
789/r/n
More:
You can find this function file_get_contents() useful but it turns everything into a string NOT an array.
file(), reads in to array
array_unique() remove the duplicates
implode() recreate the per line format
file_put_contents() write the file back