Need to read a file, search for some data and output the data using PHP
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
preg_match('!Version.*?"([^"]+)"!m', file_get_contents('/path/to/file'), $matches);
var_dump($matches);
That is fairly easy to do. Here are a few pointers:
fgets
substr
or, if you prefer,preg_match
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;
?>
精彩评论