AlanComek
AlanComek

Reputation: 3

Extracting a string on a file that gets randomly edited

So, lets say that I have a text file that contains some information on a client.

Name=RichardBaxton
Age=21
Nicks=ReconRoverRick
Company=MorganIndustries

Obviously there are better ways to store information about people, but for the sake of the situation, I'll say that I'm a stubborn idiot who wants it to be like this.

What I am trying to do is extract the "ReconRoverRick" part, and put it in a string (Lets say, $nick). However, since the file is always changing, and it always changes at random, I have no clue on how to do this. The Nicks=ReconRoverRick part could change to Nicks=KingBax or something.

Can I get some help?

Upvotes: 0

Views: 38

Answers (2)

James
James

Reputation: 119

It looks like the file you're parsing is very similar format to an ini file.

You could try using php's built-in function parse_ini_file (link)

Example:

$ini_array = parse_ini_file('path/to/your/file.txt');
echo $ini_array['Nicks']; // will output ReconRoverRick/KingBax

Upvotes: 1

PhilMasteG
PhilMasteG

Reputation: 3185

if (preg_match("/^Nicks=(.+)$/", "Nicks=ReckonRoverRick", $matches)) { print $matches[1]; }

Instead of "Nicks=ReckonRoverRick" use file_get_contents("filename.txt") where filename.txt is the name of the text-file containing this information.

Upvotes: 0

Related Questions