PHP Array file_get_contents for each query - php

Using the following code:
<?php
$url = $_GET['name'];
$nurl = str_replace('%3B', ';', $url);
$arr = explode("=", $nurl);
$rValue = parse_url($nurl, PHP_URL_QUERY);
preg_match_all('/\w+=.*/',$rValue,$matches);
parse_str($matches[0][0], $output);
parse_str($rValue);
echo $rValue;
?>
I get the following output:
field1=6918795;6990788;21434586&person144453469&number1=7412127;11425470;31104141&person86762935&number2=9152334;26300968;26441141&person38579423&number3=7897334;9114514;11656368;13683203
The question:
How to get file get contents from an URL for each query?
Example:
File get contents for field1 (of which the number 1 is a variable)
File get contents for 6918795
File get contents for 6990788
File get contents for 21434586
File get contents for person144453469 (the number 144453469 is a variable)
etc.

$var = field1=6918795;6990788;21434586&person144453469&number1=7412127;11425470;31104141&person86762935&number2=9152334;26300968;26441141&person38579423&number3=7897334;9114514;11656368;13683203
First separate by &
$separated_by_ands = explode('&', $var) ;
foreach($separated_by_ands as $separated_by_and) {
$separated_by_equals = explode('=', $separated_by_and);
$separated_by_equals = $separated_by_equals[1];
$separated_by_semicolons = explode(';', $separated_by_equals);
foreach($separated_by_semicolons as $number) {
//do file get contents for $number
}
}
yes I know bad choice of variable names :P

Related

How can I edit the content of a ini file?

I would like to be able to edit a config file for a server application using php. The config file is as follows:
include=sc_serv_public.conf
streamid_2=2
streampath_2=/relay
streamrelayurl_2=http://<full_url_of_relay_including_port>
;allowrelay=0
;allowpublicrelay=0
I would like to edit the line:
streamrelayurl_2=http://<full_url_of_relay_including_port>
and then save the file.
I am currently using:
$data = file_get_contents("sc_serv.conf"); //read the file
$convert = explode("\n", $data); //create array separate by new line
to open the file, but now I dont know how to edit it.
As an alternative, you could just use file() instead. This just loads it up into array form, no need to explode. Then after that, you just loop the elements, if the desired needle is found, overwrite it, the write the file again:
$data = file('sc_serv.conf', FILE_IGNORE_NEW_LINES); // load file into an array
$find = 'streamrelayurl_2='; // needle
$new_value = 'http://www.whateverurl.com'; // new value
foreach($data as &$line) {
if(strpos($line, 'streamrelayurl_2=') !== false) { // if found
$line = $find . $new_value; // overwrite
break; // stop, no need to go further
}
}
file_put_contents('sc_serv.conf', implode("\n", $data)); // compound into string again and write
You can use file() to read the file content to an array, then you can iterate trough the array with foreach() searching with the strstr() function the line that have your URL (in this case is in the var $id_change) and change the value. Then as you found what you needed, you end the foreach() with break. And make your string to save in the file with implode() and save the string to the config file with file_put_content().
See the code:
<?php
$new_url = 'http://www.google.com';
$id_change = 'streamrelayurl_2';
$file = "sc_serv.conf";
$data = file($file); //read the file
foreach($data as $key => $value) {
if(strstr($value, $id_change)) {
$info = $id_change . '=' . $new_url . "\n";
$data[$key] = $info;
break;
}
}
$data = implode("", $data);
file_put_contents($file, $data);
?>
Output:
include=sc_serv_public.conf
streamid_2=2
streampath_2=/relay
streamrelayurl_2=http://www.google.com
;allowrelay=0
;allowpublicrelay=0

Uploading and downloading a 2 dimensional array using file_put_contents file_get_contents - php

Hopefully, I won't be a novice for long. I have a 2-dimensional array (simplified) that I'm trying to work with. Simply pulling out, adding to, and uploading a file record. Can somebody forgive my ignorance and explain what I'm doing wrong?:
<?php
// Updating Current Number of Vendors
$vendorcount = #file_get_contents('../Keys/VendorCount/$v');
if(isset($vendorcount))
{
$new_vendor_number = ($vendorcount + 1);
$n = $new_vendor_number;
}
else
{
$vendorcount = 0;
$new_vendor_number = 1;
$file = '../Keys/VendorCount/$v' ;
file_put_contents($file, $vendorcount) ;
};
//getting record from file
$record = file_get_contents('../Vendors/$vendorlist');
//adding new information to record array
$record[$n] = array($new_vendor_number, $catname);
//uploading updated record
$file = '../Vendors/$vendorlist' ;
file_put_contents($file, $record) ;
?>
You need to use serialize() and unserialize() in order to store an array to a file or restore it from a file. Like this:
$array = array(1,2,3);
// writing to file
file_put_contents('file.txt', serialize($array));
// restoring from file
$array = unserialize(file_get_contents('file.txt'));
This will work with arrays of any dimension.

Is there an alternative for file_get_contents?

I tried this but getting only one character from file comments.txt
I want random lines one by one .. file_get_contents disabled also urlencode
$f_contents = file_get_contents("comments.txt");
$line = $f_contents[array_rand($f_contents)];
$messages = $line;
$messages = urlencode($messages);
You can simplify it like ..
<?php
$arr = file('comments.txt',FILE_IGNORE_NEW_LINES);
shuffle($arr);
foreach($arr as $v)
{
echo $v."<br>";
}
The above code prints random lines from your text file one by one.

Reading text after a certain character in php

Okay so I have a text file and inside of the text file I have these lines:
IP = 127.0.0.1
EXE = Client.exe
PORT = 8080
TITLE = Title
MAINT = False
MAINT-Message = This is the message.
what I am wanted to do is get the 'False' part on the fifth line.
I have the basic concept but I can't seem to make it work. This is what I have tried:
<?php
$file = file_get_contents('LauncherInfo.txt');
$info = explode(' = ', $file);
echo $info[5];
?>
And with this I get a result but when I echo $info[5] it gives me 'False Maint-Message' so it splits it but it only splits at the = sign. I want to be able to make it split at the where I have pressed enter to go onto the next line. Is this possible and how can I do it?
I was thinking it would work if I make it explode on line one and then do the same for the second line with a loop until it came to the end of the file? I don't know how to do this though.
Thanks.
I think you're looking for the file(), which splits a file's contents into an array of the file's lines.
Try this:
$file = file('LauncherInfo.txt');
foreach ($file as $line) {
if ($line) {
$splitLine = explode(' = ',$line);
$data[$splitLine[0]] = $splitLine[1];
}
}
echo $data['MAINT'];
Just in case you were curious, since I wasn't aware of the file() function. You could do it manually like this
<?php
$file = file_get_contents('LauncherInfo.txt');
$lines = explode("\n", $file);
$info=array();
foreach($lines as $line){
$split=explode(' = ',$line);
$info[]=$splitline[1];
}
echo $info[5];//prints False
?>

how to display context of .cfg file in php?

I have a .cfg file which contains some code. I would like to display the whole .cfg in php page. Code for my_config.cfg is here:
# ATA/IDE/MFM/RLL support
#
account_name=changl
#
# IDE, ATA and ATAPI Block devices
#
CONFIG_BLK_DEV_IDE=y
CONFIG_BLK_DEV_IDEDISK=y
CONFIG_BLK_DEV_IDECD=n
Now I wrote a php code which checks the condition and display what i write in echo. But instead of that I want to display everything from the file. Here is the php code:
<?php
$config_file = "my_config.cfg";
$comment = "#";
$fp = fopen($config_file, "r");
while (!feof($fp)) {
$line = trim(fgets($fp));
if ($line && !preg_match("/^$comment/", $line)) {
$pieces = explode("=", $line);
$option = trim($pieces[0]);
$value = trim($pieces[1]);
$config_values[$option] = $value;
}
}
fclose($fp);
if ($config_values['account_name'] == "changl")
echo "account_name is changl";
else
echo "account_name is not changl";
?>
The code is working properly. but I want to display the data in the file. Please any help is appreciated.
The easiest way is to use parse_ini_file():
$config_values = parse_ini_file('my_config.cfg');
After that you can work with $config_values like with any other regular array

Categories