I tried to include the php file in array like that but it is not working
username.php file is : "mohammed", "ahmed", "hafian", "gimmy", "osama"
and the main file is : $usernames = array(include 'usernames.php');
In usernames.php do:
<?php
return ["mohammed", "ahmed", "hafian", "gimmy", "osama"];
In foo.php do:
$usernames = include 'usernames.php';
RTM: https://www.php.net/manual/en/function.include.php#example-145
You will need to open and read the file, you can't just include and expect it to parse the file and store into your array. Also, can you make usernames.php a text file and not PHP code? You don't want to be parsing PHP code. I think you'll need to approach it like this:
`<?php
$fn = fopen("usernames.txt","r");
while(! feof($fn)) {
$result = fgets($fn);
# parse line and append to array
}
fclose($fn);
?>`
Related
Is there anyway to access variables of one php file into another?
- I am trying to validate a form. I need to access variables from the validationConditions.php file in form.php file.
I have tried creating sessions but they are error prone.
I am using the latest version of dreamweaver.
Is it possible to use $_POST to achieve the results?
Any example will be welcome...
Thanks a lot in advance.
The easiest would be to use session variables. In the first page you could set the values like this
session_start();
$_SESSION['myvar'] = 'My Data';
Then in the second page you can retrieve the data like this...
session_start();
$myvar = $_SESSION['myvar'];
Which in turn sets $myvar to "My Data"
You can read a php file with another php file. Assume that we have 2 php files.
new.php
<?php
$a="something";
?>
serach.php ---> search in new.php
<?php
// What to look for
$search = '$a'; //WE are searching $a
$lines = file('new.php'); // in new.php
foreach($lines as $line)
{
// Check if the line contains the string we're looking for, and print if it does
if(strpos($line, $search) !== false)
echo $line;
}
?>
Something like that. You have to edit this file.
A somehow devious solution:
ob_start();
$request = array(
"_id"=>"0815",
"user"=>"john",
"email"=>"john#dunbar.com",
"phone"=>NULL
); // Don't know. Just how u need your request to look like.
include('validationConditions.php');
$response = getValidationConditions($request); // Function in 'validationConditions.php' that responds an array or a JSON
$out = ob_get_clean();
echo json_encode($out);
i have a problem i couldn't figure out since im self-taught and still exploring the php world
so i have a text file that looks like this:
951753159
456787541
123156488
748651651
and i got an url with a variable
http://example.com/mypage.php?variable=951753159
what i want is to check if the url variable matches one of the txt file lines in order to execute a code
i already tried this
$search = $_GET["variable"];
$file = "variables.txt";
if (preg_match('/^' . $search . '$/m', file_get_contents($file))) {
THE CODE THAT I WANT TO EXECUTE
}
but for some reason it matches the whole content of the file
any help is highly appreciated
Thanks in advance :)
Try with an array from file():
$lines = file("variables.txt", FILE_IGNORE_NEW_LINES);
if(in_array($_GET["variable"], $lines)) {
// YES FOUND
} else {
// NOT FOUND
}
From the documentation on `file_get_contents', the entire contents of the file are read as a string. So that is why it is matching against the entire file.
The command that you want to use is file, this reads the file into an array of each line.
I would
Use file to read the file into an array.
Then array_flip the array so that it's values are now the keys
Which allows me to isset($array[$key])
You can do this.
<?php
#$search = $_GET["variable"];
$search = '123156488';
$file_txt = "content.txt";
$file = file($file_txt);//convert the txt in array
foreach ($file as $key => $value) {
if (trim($search) == trim($value)) {
print "DO something! " . $value;
}
}?>
Regards.
Nelson.
PHP
When using a txt included file the explode fails. The txt file is just this: a,b,c,d,e
When not an include the string 'explodes' into an array.
$data = include("data.txt");
settype($data,"string");
print "<br><br>type: ".gettype($data)."<br>";
$data = explode(",",$data);
print_r($data);
include() and require() are meant to be used with PHP scripts.
For importing raw data, consider file_get_contents(), which will return a string.
Try
$data = file_get_contents("data.txt");**
print "<br><br>type: ".gettype($data)."<br>";
$data = explode(",",$data);
print_r($data);
The .php file contains code like:
<?php
return array(
// commments...
'some_item' => 'abc',
// commments...
'some_other_item' => array(1, 2, 3),
...
);
What's the best way to "parse" this file somehow from within my PHP application, and be able to update data in it, without breaking formatting, code etc. ?
$content = require($file); will get the file's content (beware of relative paths and requirevs include semantics).
file_put_contents($file, '<?php return ' . var_export($content, true) . ';'); will write the content back to the file, but formatting will change. If you really need to keep the formatting, you could look into PHP Beautifier. It's not a perfect solution though.
simply include() the file:
$my_array = include 'myarray.php';
See example #4 at http://php.net/manual/en/function.include.php and http://php.net/manual/en/function.return.php
The best way is to include it; yes, include returns a value!
$data = include 'someData.php';
Assuming that your array has only two items
<?php
$myArray=include 'data.php';
$myArray[count($myArray)]='Item-3';
echo $myArray[2]; // Item-3
?>
Are you trying to load filenames which are contained in an array? If so:
$array_of_files = array('header.php','database.php','../inc/other_stuff.php');
foreach($array_of_files as $file)
{
include $file;
}
I have a file called config.php, I want it to remain exactly as is, however on Line # 4 there is a line saying:
$config['url'] = '...could be anything here';
I'd like to only replace the contents of line # 4 with my own url provided for $config['ur'], is there a way to do this in PHP?
Since you know the exact line number, probably the most accurate way to do this is to use file(), which returns an array of lines:
$contents = file('config.php');
$contents[3] = '$config[\'url\'] = "whateva"'."\n";
$outfile = fopen('config.php','w');
fwrite($outfile,implode('',$contents));
fclose($outfile);
$myline = "my confg line";
$file = "config.php";
$contents = file($file);
$contents[3] = $myLine;
$file = implode("\n", $contents);
Either create another config (myconfig.php). Include the original and overwrite the option. include myconfig.php instead of the original one.
OR
Go to where config is included (you did use a single place for all your includes right?) and set the config option there.
include "config.php"
$config['url'] = "my_leet_urlz";
you could read the file, use str_replace or preg_replace on the appropriate strings, then write the file back to itself.
$filename = "/path/to/file/filename";
$configFile = file($filename);
$configFile[3] = '$'."config['url'] = '...could be anything here';";
file_put_contents($filename ,$configFile);
If there is only one $config['url'], use a preg_replace()
http://us.php.net/preg_replace
something like:
$pattern = '\$config['url'] = "[\d\w\s]*"'
$file_contents = preg_replace($pattern, $config['url'] = "my_leet_urlz";,$file_contents);
you'll need to fix the pattern as I'm new at regex and I know that's not right.