Find the answer to your Linux question:
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 ...
  1. #1
    Just 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:
    SERVER=canfs01
    SHARE=public
    USERNAME=guest
    PASSWORD=
    I am hoping to get the following output so I can put it in individual input text boxes:
    canfs01
    public
    guest
    The closest I've come to achieving this is the following in php:
    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>
    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.

    Any help and pointers you could give me would be greatly appreciated.

    Cheers,
    Griffo

  2. #2
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,097
    Maybe this is what you want
    PHP: parse_ini_file - Manual
    You must always face the curtain with a bow.

  3. #3
    Just 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 luck
    Last edited by r31griffo; 08-15-2010 at 12:06 PM. Reason: Edited post to add hyperlinks

  4. #4
    Linux Newbie
    Join Date
    Apr 2010
    Location
    Novosibirsk, Russia
    Posts
    136

    Post

    It's quite simple. Just use 'split' call twice. Read all data into single string and at first split it by lines
    Code:
     $lines = split("\n", $all_data_as_string);
    and then split each line by "=" delimiter inside foreach
    Code:
    foreach($lines as $str) { $pair = split("=", $str); }
    just catch $pair[1] every time

  5. #5
    Just 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

    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 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...

    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?

  6. #6
    Just 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="); ?> > 

  7. #7
    Linux Newbie
    Join Date
    Dec 2009
    Posts
    241
    Well I prefer a conventional programm Stile and would define following:
    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); 
    I am not sure the "while (($line = fgetcsv($fh,100,"=")) == true)" will work ... would need to investigate more into it again ...

    And to tell you the truth ... I would change the names of the fields ... like:
    PHP Code:
    echo $write[$line[0]]."<input type='text' name='".$line[0]."' value='".trim($line[1])."'>\n<br>"
    That way you can easily make an script writing the values back:
    PHP Code:
    foreach ($_POST as $idname => $value){
    if (isset(
    $write[$idname])){
     
    fwrite($fh$idname."=".$value."\n");
    }


  8. #8
    Just 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...