Results 1 to 8 of 8
G'day everyone,
I'm trying to make a webpage that will display the bash variables I have in a file. These variables are used in a bash script that is run ...
- 08-15-2010 #1Just Joined!
- Join Date
- Jan 2010
- Location
- Sydney, Australia
- Posts
- 51
Stripping characters before '=' delimiter in PHP
G'day everyone,
I'm trying to make a webpage that will display the bash variables I have in a file. These variables are used in a bash script that is run from on my server.
The file looks like this:
I am hoping to get the following output so I can put it in individual input text boxes:SERVER=canfs01
SHARE=public
USERNAME=guest
PASSWORD=
The closest I've come to achieving this is the following in php:canfs01
public
guest
I can't figure out how to strip the variable $line of everything before (and including) '='. I'm using PHP Version 5.2.6-1.PHP Code:<?php
$fh = fopen("test.conf", "r");
?>
<?php
$line = fgets($fh);
?>
Server name: <input type='text' name='HOST' value=<?php echo $line; ?>
<br>
<?php
$line = fgets($fh);
?>
Share name: <input type='text' name='HOST' value=<?php echo $line; ?>
<br>
<?php
$line = fgets($fh);
?>
User name: <input type='text' name='HOST' value=<?php echo $line; ?>
<br>
<?php
$line = fgets($fh);
?>
password: <input type='text' name='HOST' value=<?php echo $line; ?>
<br>
Any help and pointers you could give me would be greatly appreciated.
Cheers,
Griffo
- 08-15-2010 #2
Maybe this is what you want
PHP: parse_ini_file - ManualYou must always face the curtain with a bow.
- 08-15-2010 #3Just Joined!
- Join Date
- Jan 2010
- Location
- Sydney, Australia
- Posts
- 51
I had a look at that page but I'm not sure how I could use that to achieve my outcome.
There are a couple of other commands that I've been playing with:
preg_match
parse_str
but so far I've had no luckLast edited by r31griffo; 08-15-2010 at 12:06 PM. Reason: Edited post to add hyperlinks
- 08-15-2010 #4Linux Newbie
- Join Date
- Apr 2010
- Location
- Novosibirsk, Russia
- Posts
- 136
It's quite simple. Just use 'split' call twice. Read all data into single string and at first split it by lines
and then split each line by "=" delimiter inside foreachCode:$lines = split("\n", $all_data_as_string);just catch $pair[1] every timeCode:foreach($lines as $str) { $pair = split("=", $str); }
- 08-15-2010 #5Just Joined!
- Join Date
- Jan 2010
- Location
- Sydney, Australia
- Posts
- 51
Thank you for your suggestion, but I found the answer just before I saw your post...I got the idea from a post on PHP: trim - Manual page
I know it's not very pretty at the moment, but I'll put this in a table that I'll create out of HTML, it will look pretty good when I'm done...PHP Code:<?php
function trimString($input, $string){
$input = trim($input);
$startPattern = "/^($string)+/i";
$endPattern = "/($string)+$/i";
return trim(preg_replace($endPattern, '', preg_replace($startPattern,'',$input)));
}
?>
<?php
$fh = fopen("test.conf", "r");
?>
<?php
$line = fgets($fh);
?>
Server name: <input type='text' name='HOST' value=<?php echo trimString($line, "SERVER="); ?> >
<br>
<?php
$line = fgets($fh);
?>
Share name: <input type='text' name='HOST' value=<?php echo trimString($line, "SHARE="); ?> >
<br>
<?php
$line = fgets($fh);
?>
Username: <input type='text' name='HOST' value=<?php echo trimString($line, "USERNAME="); ?> >
<br>
<?php
$line = fgets($fh);
?>
Password: <input type='text' name='HOST' value=<?php echo trimString($line, "PASSWORD="); ?> >
<br>
I have 1 last question though, how to I change the output of the PASSWORD field to something like *****, but if there is no password I'd like the field to come up empty...any ideas?
- 08-15-2010 #6Just Joined!
- Join Date
- Jan 2010
- Location
- Sydney, Australia
- Posts
- 51
never mind, I found that if I change the 'type' in the input type field to 'password' I get the result I'm looking for eg:
HTML Code:Password: <input type='password' name='HOST' value=<?php echo trimString($line, "PASSWORD="); ?> >
- 08-15-2010 #7Linux Newbie
- Join Date
- Dec 2009
- Posts
- 241
Well I prefer a conventional programm Stile and would define following:
I am not sure the "while (($line = fgetcsv($fh,100,"=")) == true)" will work ... would need to investigate more into it again ...PHP Code:$write["SERVER"]="Server name: ";
$write["SHARE"]="Share name: ";
$write["USERNAME"]="Username: ";
$fh = fopen("test.conf", "r");
while (($line = fgetcsv($fh,100,"=")) == true){
echo $write[$line[0]]."<input type='text' name='HOST' value='".trim($line[1])."'>\n<br>";
}
fclose($fh);
And to tell you the truth ... I would change the names of the fields ... like:
That way you can easily make an script writing the values back:PHP Code:echo $write[$line[0]]."<input type='text' name='".$line[0]."' value='".trim($line[1])."'>\n<br>";
PHP Code:foreach ($_POST as $idname => $value){
if (isset($write[$idname])){
fwrite($fh, $idname."=".$value."\n");
}
}
- 08-16-2010 #8Just Joined!
- Join Date
- Jan 2010
- Location
- Sydney, Australia
- Posts
- 51
I like your idea using the array in the naming of the input box and value in one go. I will research fgetcsv and see if I can get this to work...This would automatically increase in size if I add more variables to the file down the track...interesting, you have me thinking.


Reply With Quote