tags:

views:

34

answers:

3

Hello...I am new to PHP (never really used it before tonight) and I need to use a PHP script to read the contents of a file on my website (http://kylemills.net/Geocaching/BadgeGen/MacroFiles/BadgeGenBeta.gsk) and then take some specific varying data and output it.

For example: If my file contains the text:


random text
Here is some text
#There is some text here too
$Version = "V2.2.23 Beta"
random text
#some more text
$Text="some text"

If the above was the contents of my file, I need the script to return "V2.2.23 Beta" (without quotes). The idea is that as I update the file, the version changes and I want this to be reflected across my site.

I am sorry if I don't make any sense...any help would be appreciated :)

-Thanks so much, Kyle

+1  A: 

That is fairly easy to do. Here are a few pointers:

Artefacto
+2  A: 
preg_match('!Version.*?"([^"]+)"!m', file_get_contents('/path/to/file'), $matches);
var_dump($matches);
zerkms
note that file_get_contents() is php5 only so he'd need php5.. probably does, but it's worth mentioning.
danieltalsky
@danieltalksy: indeed, but php4 has died long ago, so i never suppose it can still be used though
zerkms
some hosts, such as yahoo, still only support php4...
Dan Heberden
@Dan Heberden: and some hosts still only support php3 ;-) Developers should choose the hosting that provides all software that they need, not in an opposite way. We are living in 2010, let's stop using the software deprecated in 2008.
zerkms
Oh quite right - I'd not be caught dead using a php4 or lower if given a choice, just thought i'd mention that php4, unfortunately, is still alive and kicking. It does, however, need one of those php4 hotels where it checks in and never checks out....
Dan Heberden
until developers adopt their code to all possible versions instead of just following currently actual - hosting providers will never migrate ;-)
zerkms
Hello...thanks for the response, however, it seems to return a long string (Which includes my data) but is not only my data. This is what it returns:<pre><code>array(2) { [0]=> string(24) "Version = "V2.2.23 Beta"" [1]=> string(12) "V2.2.23 Beta" } 1</pre></code>Any suggestions?Thanks
Kyle
@Kyle: `$matches[1]` is what you need. And please - take a time to read php basics ;-)
zerkms
A: 

simple. just update (inside the file) "$Version = 'V2.2.23 Beta'" to "<?php $Version = 'V2.2.23 Beta'; ?>"

then, wherever you want to use it:

<?php 
echo '<!--';
include 'myfile.txt';
echo '-->';
echo $Version;
?>
tann98