locked
Read a variable from a TXT file? RRS feed

  • Question

  • Is there a way to read a variable stored in a txt file...maybe PHP?....well i guess thats the wrong question, because im sure there is a way, the question is more "How?" Smile

     

    For example, if I had a php file with the text [$myVar = "HELLO!!!!"] how could detect "HELLO!!!!" using code?

     

    Thanks,

    paoloTheCool

     

     

    Sunday, July 8, 2007 6:26 PM

Answers

  • Hi...

     

    Code Snippet

    using System;

    using System.Text.RegularExpressions;

     

    namespace ConsoleApplication1

    {

        class Program

        {

            static string stringPattern =

                @"((""([^""]|(\\""))*"")|('[^']*'))";

     

            static Regex phpRegex = new Regex(

                @"(?<=\$\w+\s*=\s*)" + stringPattern,

                RegexOptions.Multiline);

     

            static void Main()

            {

                // TODO: read the file contents

                foreach(Match m in phpRegex.Matches(phpFileContent))

                {

                    Console.WriteLine(m.Value);

                }

            }

        }

    }

     

     

    The pattern will match the following strings:

    • "Hello world"
    • 'Hello world';
    • "Hello 'world'"
    • 'Hello "world"'
    • "Hello \"world\""

    Keep in mind, that you can concatenate strings in PHP, e.g.

     

    Code Snippet
    $myVar = "Hello " . "world";

     

    The pattern will not match those concatenations.

     

    --

    Regards,

    Daniel Kuppitz

    Sunday, July 8, 2007 8:24 PM
  • There are a couple of ways. One is to go through each line of the textfile and check to see if the line contains that variable. The other, and more correctly, is to use Regex - Regular Expressions. The best place to ask Regex questions would be here:

     

    http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=1212&SiteID=1

     

    if you are going for the previous option stated, here is an example:

     

    using (StreamReader reader = new StreamReader(filename))

    {

       while (reader.EndOfStream == false)

       {

          if (reader.ReadLine().Contains("ValueHere"))

          {

             //value has been found

          }

       }

    }

     

    I hope this helps!

     

    edit: looks like we both responded at the same time ;-)

    Sunday, July 8, 2007 8:27 PM
  • I don't know anything about php files but if it is just text, I would read it into a string variable like this:

     

    Code Snippet

                string fileData = File.ReadAllText(filename);

     

    Then search the string for a pattern using a regular expression like this:

     

    Code Snippet

                Regex r = new Regex("(\\$myVar)(\\s*=\\s*\")(\\S*)(\")");

                Match m = r.Match(fileData);

                Group g = m.Groups[3];

                label1.Text = g.ToString();

     

    This regular expression looks for the pattern '$myVar' followed by any number of white spaces followed by '=' followed by more white spaces and a ' " ' character then and number of non white space characters followed by a final ' " ' character.

     

    The regular expression is applied to the string of characters from your text file to get a match.

     

    The group 'g' is used to pull out only the portion of the match that corresponds to the data we are interested in.

     

    Finally the g object is converted to a string for output (or for whatever you want to do with it).

     

    For more info on regular expressions, see this and this.

     

    Sunday, July 8, 2007 8:34 PM

All replies

  • Hi...

     

    Code Snippet

    using System;

    using System.Text.RegularExpressions;

     

    namespace ConsoleApplication1

    {

        class Program

        {

            static string stringPattern =

                @"((""([^""]|(\\""))*"")|('[^']*'))";

     

            static Regex phpRegex = new Regex(

                @"(?<=\$\w+\s*=\s*)" + stringPattern,

                RegexOptions.Multiline);

     

            static void Main()

            {

                // TODO: read the file contents

                foreach(Match m in phpRegex.Matches(phpFileContent))

                {

                    Console.WriteLine(m.Value);

                }

            }

        }

    }

     

     

    The pattern will match the following strings:

    • "Hello world"
    • 'Hello world';
    • "Hello 'world'"
    • 'Hello "world"'
    • "Hello \"world\""

    Keep in mind, that you can concatenate strings in PHP, e.g.

     

    Code Snippet
    $myVar = "Hello " . "world";

     

    The pattern will not match those concatenations.

     

    --

    Regards,

    Daniel Kuppitz

    Sunday, July 8, 2007 8:24 PM
  • There are a couple of ways. One is to go through each line of the textfile and check to see if the line contains that variable. The other, and more correctly, is to use Regex - Regular Expressions. The best place to ask Regex questions would be here:

     

    http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=1212&SiteID=1

     

    if you are going for the previous option stated, here is an example:

     

    using (StreamReader reader = new StreamReader(filename))

    {

       while (reader.EndOfStream == false)

       {

          if (reader.ReadLine().Contains("ValueHere"))

          {

             //value has been found

          }

       }

    }

     

    I hope this helps!

     

    edit: looks like we both responded at the same time ;-)

    Sunday, July 8, 2007 8:27 PM
  • I don't know anything about php files but if it is just text, I would read it into a string variable like this:

     

    Code Snippet

                string fileData = File.ReadAllText(filename);

     

    Then search the string for a pattern using a regular expression like this:

     

    Code Snippet

                Regex r = new Regex("(\\$myVar)(\\s*=\\s*\")(\\S*)(\")");

                Match m = r.Match(fileData);

                Group g = m.Groups[3];

                label1.Text = g.ToString();

     

    This regular expression looks for the pattern '$myVar' followed by any number of white spaces followed by '=' followed by more white spaces and a ' " ' character then and number of non white space characters followed by a final ' " ' character.

     

    The regular expression is applied to the string of characters from your text file to get a match.

     

    The group 'g' is used to pull out only the portion of the match that corresponds to the data we are interested in.

     

    Finally the g object is converted to a string for output (or for whatever you want to do with it).

     

    For more info on regular expressions, see this and this.

     

    Sunday, July 8, 2007 8:34 PM
  • is there a "using" that I need to have, b/c there are errors when I try to build?

     

    Thanks,

    paoloTheCool

    Monday, July 9, 2007 12:26 AM
  • using System.Text.RegularExpressions;

     

    and

     

    using System.IO;

     

    When you get an error like that, Visual Studio will help you out.  Click on the type declaration that is giving you the error.  You should see a small rectangle at the end of the word.  If you hover over or around the rectangle, you will see a dropdown that will suggest a 'using' statement.  If you click on the suggestion, it will be added for you automatically.

     

     

    Monday, July 9, 2007 12:59 AM
  • Hmmm Im probably being really dumb right now.....Im trying to do an internet detection function by loading a variable from a txt file that is uploaded to the web.

    Is there a way just to find out if that file exists?

     

    Thanks,

    paoloTheCool

    Monday, July 9, 2007 3:57 PM