You are not logged in.

  • Login

1

Wednesday, April 6th 2011, 1:03am

str_replace & Include

Nabend,
Da wär ich nochmal mit nem anderem Problemchen,
und zwar habe ich ein Script, dass einen Quake3 Server Status abfragt, dies ist auch alled kein Problem, das script ist übrigens GameQ.
Dieses Script Liefert einzelne Befehle bzw Variabeln zurück, beispiel sv_hostname "IrgendeinName"
da unteranderem auch irgendwelche anderen Variabeln mit gesendet werden, die ich jedoch nicht brauche ala uptime und andere dinge, möcht eich diese rausfiltern,

da dacht ich mir ich versuchs mit str_replace, jedoch ohne erfolg, der mist steht immer noch da...

Perl Quellcode

1
2
3
4
<?php
$var1 = include('gameq/example.php');
$var = str_replace("Uptime", " ",$var1);
?>


Hätte diesbezüglich jemand eine Idee für mich?

MFG

2

Wednesday, April 6th 2011, 1:17am

PHP Quellcode

1
unset($nicht_benoetigte_variable);

Eine Zuweisung des inkludierten Inhalts an eine Variable ist Blödsinn. Die Datei wird eingelesen für den Interpreter und somit kann der eigentliche Inhalt keiner Variable zugewiesen werden, es sei denn mittels ob_get_contents etc. Wenn du so etwas wie

PHP Quellcode

1
$var = include('var');
machst und dann

PHP Quellcode

1
print $var;
erhälst du das integer Äquivalent des boolschen Ausdrucks true, also 1 (Sofern die Datei eingelesen werden konnte, ansonsten gibt es eh einen error).
Wenn du einen Dateiinhalt haben möchtest, solltest du dir file_get_contents angucken. Du könntest dir den Inhalt holen, mit str_replace (wie du es schon tust) ersetzen und dann den restlichen Inhalt als Code mittels evalausführen, aber das wäre dann doch eher ein Umweg.

3

Wednesday, April 6th 2011, 3:22am

Ich glaube wir haben uns Falsch verstanden.

meine Sidebar.php soll dass script gameq/example.php auslesen
bzw Includen...
nur sollen bei dem includen mehrere sachen replaced werden, da einige ding enicht benötigt und angezeigt werden sollen, mit file_get_contents bekomme ich nur den Source der Datei!

4

Wednesday, April 6th 2011, 11:22am

Wie rswhite schon geschrieben hat ist include keine Funktion sondern ein Sprachkonstrukt.

PHP Quellcode

1
2
$var1 = include('gameq/example.php'); // fail
include 'gameq/example.php'; // well done


Mit include bindet der Interpreter den Inhalt der zu includierenden Datei einfach ein. Ganz banal kann man es sich wie ein copy+paste vorstellen.


Wenn du also auf Variablen aus der Datei zugreifen möchtest brauchst du nix anderes zu tun als diese einfach zu benutzen ;)


Beispiel:

Datei A.php

PHP Quellcode

1
$baem = "Hello world";

Datei B.php

PHP Quellcode

1
include "A.php"; $baem .= "!";

Datei C.php

PHP Quellcode

1
include "B.php"; echo $baem; // gibt "Hello world!" aus



ps.
wenn du über file_get_contents gehst/gehen möchtest, dann schau dir eval an.

5

Wednesday, April 6th 2011, 12:15pm

Was steht den in deiner gameq/example.php konkret drin? Kannst du vielleicht einen Ausschnitt zeigen und dann anhand von dem Ausschnitt erklären was da z.B. verändert werden soll.

6

Wednesday, April 6th 2011, 12:43pm

gameq/example.php

PHP Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
error_reporting(E_ALL);
 
 
require_once 'GameQ.php';
 
 
// Define your servers,
// see list.php for all supported games and identifiers.
$servers = array(
    'server 1' => array('quake3', '78.46.59.14', 27980),
);
 
 
// Call the class, and add your servers.
$gq = new GameQ();
$gq->addServers($servers);
 
 
// You can optionally specify some settings
$gq->setOption('timeout', 900);
 
 
// You can optionally specify some output filters,
// these will be applied to the results obtained.
$gq->setFilter('normalise');
$gq->setFilter('sortplayers', 'gq_ping');
 
// Send requests, and parse the data
$results = $gq->requestData();
 
 
 
 
 
 
// Some functions to print the results
function print_results($results) {
 
    foreach ($results as $id => $data) {
 
        printf("<h2>%s</h2>\n", $id);
        print_table($data);
    }
 
}
 
function print_table($data) {
 
    $gqs = array('gq_online', 'gq_address', 'gq_port', 'gq_prot', 'gq_type');
 
 
    if (!$data['gq_online']) {
        printf("<p>The server did not respond within the specified time.</p>\n");
        return;
    }
 
    print("<table><thead><tr><td>Variable</td><td>Value</td></tr></thead><tbody>\n");
 
    foreach ($data as $key => $val) {
 
        if (is_array($val)) continue;
 
        $cls = empty($cls) ? ' class="uneven"' : '';
 
        if (substr($key, 0, 3) == 'gq_') {
            $kcls = (in_array($key, $gqs)) ? 'always' : 'normalise';
            $key = sprintf("<span class=\"key-%s\">%s</span>", $kcls, $key);
        }
 
        printf("<tr%s><td>%s</td><td>%s</td></tr>\n", $cls, $key, $val);
    }
 
    print("</tbody></table>\n");
 
}
 
 
 
 
 
 
 
 
?>
 
<?php
    print_results($results);
?>


Diese Datei bringt Folgende Ausgabe im Browser.

Quoted

server 1
Variable Value
EnhancedMod 1.0.7
P 22333333332133312333333332313332333333233333333211211121111-122
balancedteams 1
clients 0
friendlyFire 0
g_alliedmaxlives 0
g_antilag 1
g_axismaxlives 0
g_balancedteams 1
g_bluelimbotime 12000
g_friendlyFire 0
g_gametype 2
g_heavyWeaponRestriction 100
g_maxlives 0
g_minGameClients 8
g_needpass 0
g_redlimbotime 15000
g_voteFlags 0
game jaymod
gamename et
gametype 2
gq_address 78.46.59.14
gq_dedicated
gq_gametype 2
gq_hostname ^3BEGINNERS XP SAVE - STOCK MAPS
gq_mapname fueldump
gq_maxplayers 64
gq_mod jaymod
gq_numplayers 62
gq_online 1
gq_password 0
gq_port 27980
gq_prot quake3
gq_type quake3
hostname ^3BEGINNERS XP SAVE - STOCK MAPS
mapname fueldump
maxlives 0
mod_binary linux-release
mod_url http://jaymod.clanfu.org
mod_version 2.1.7
needpass 0
omnibot_enable 1
omnibot_playing 0
protocol 82
punkbuster 1
pure 1
serverload 36
sv_allowAnonymous 0
sv_cpu Intel(R) Core(TM) i7 CPU
sv_floodProtect 1
sv_hostname ^3BEGINNERS XP SAVE - STOCK MAPS
sv_maxPing 0
sv_maxRate 45000
sv_maxclients 64
sv_minPing 0
sv_minguidage 0
sv_privateClients 0
sv_punkbuster 1
sv_uptime 02d09h16m
timelimit 30
version ET
voteFlags 507903
weaprestrict 100


Diese ich natürlich nicht alle brauche, deswegen wollte ich einige per str_replace wegfiltern, aber leichter gesagt, als getan :(

7

Wednesday, April 6th 2011, 1:23pm

Das ist ein Array, da brauchst du lediglich die nötigen Elemente zu löschen.


PHP Quellcode

1
2
include 'gameq/example.php';
unset($results['irgendwas']);


Schau dir einfach das Array an

PHP Quellcode

1
var_dump($results);

8

Wednesday, April 6th 2011, 1:28pm

Hatte ich unset nicht sogar erwähnt? ^^

9

Wednesday, April 6th 2011, 1:31pm

Du musst lediglich darauf achte, dass du nichts ausgibst und mit dem Array in $results arbeitest.

Den Inhalt von einzelnen Elementen kannst du dann auch beliebig bearbeiten.
z.B. $result['foo'] = $result['foo']."bar";

10

Wednesday, April 6th 2011, 2:16pm

Unset funkt leider nicht,var dump gibt nur den Array aus.
Bin solangsam echt am verzeifeln ...
habe nun schon Probiert, eine Variable zu entfernen jedoch ohne Erfolg.

PHP Quellcode

1
2
3
4
5
6
<?php
 
					include 'gameq/example.php';
					unset($results['sv_cpu']);
					print($results['g_gametype']);
					?>

11

Wednesday, April 6th 2011, 2:18pm

Die werden schon entfernt, dein Problem ist nur vermutlich, dass diese example.php zu erst die Ausgabe macht und du hinterher bestimmte Sachen in der Ausgabe entfernen willst - das geht natürlich nicht mehr. Du musst erst entfernen und dann die print_results Funktion ausführen.

12

Friday, April 8th 2011, 12:26am

Die werden schon entfernt, dein Problem ist nur vermutlich, dass diese example.php zu erst die Ausgabe macht und du hinterher bestimmte Sachen in der Ausgabe entfernen willst - das geht natürlich nicht mehr. Du musst erst entfernen und dann die print_results Funktion ausführen.



und wie stell ich dies ambesten an?
hab nun dass print aus der example php genommen, str_replace("sv_cpu"," ",$results); gemacht und in der sidebar.php include ('gamew/example.php'); sowie print $results;
das selbe mit unset versucht, jedoch kein erfolg:(

13

Friday, April 8th 2011, 3:45pm

Der nachfolgende Teil muss aus deiner example.php erstmal raus:

Source code

1
2
3
<?php
    print_results($results);
?>



Anschließend kannst du - nachdem du dein ganzes unset Zeug etc. gemacht hast- diesen Code wieder einfügen.
Also:

Source code

1
2
3
unset(...);
...
print_results($results);

14

Friday, April 8th 2011, 8:52pm

Habe es nun mit folgendem Code Probbiert:

PHP Quellcode

1
2
3
<?php include ('gameq/example.php'); ?>
                    <?php unset($results['sv_cpu']); ?>
                    <?php print_results($['gq_address']); ?>

dann:

PHP Quellcode

1
2
3
<?php include ('gameq/example.php'); ?>
                    <?php unset($data['sv_cpu']); ?>
                    <?php print_results($['gq_address']); ?>



Quoted


Notice: Undefined variable: results oder data in C:\xampp\htdocs\koks\sidebar.php on line 25

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\koks\gameq\example.php on line 40

15

Friday, April 8th 2011, 11:02pm

du musst auch print_results($results); machen und nicht irgendwas anderes :)

16

Friday, April 8th 2011, 11:39pm

Oops sry, hab den Code nur Falsch reingeschrieben, habe natürlich print_results gemacht, jedoch funzt unset nicht, jedenfalls nicht unset($results['sv_cpu'']); als beispiel oder andere variablen ;(

17

Saturday, April 9th 2011, 10:19am

PHP Quellcode

1
2
3
4
5
<?php
 include ('gameq/example.php');
 unset($results['sv_cpu']); ?>
 print_results($results);
?>


So muss das ganze aussehen. Du musst also immer mit dieser $results Variable (eigentlich ist es ein Array) arbeiten.Also auch beim unset usw.

18

Saturday, April 9th 2011, 1:11pm

geht wie gesagt nur leider nicht:( die ausgabe liefert nach wie vor

Quoted

server 1
Variable Value
EnhancedMod 1.0.7
P 23333313312333333121333212323333131333333122211221211-2-11212122
balancedteams 1
clients 0
friendlyFire 0
g_alliedmaxlives 0
g_antilag 1
g_axismaxlives 0
g_balancedteams 1
g_bluelimbotime 12000
g_friendlyFire 0
g_gametype 2
g_heavyWeaponRestriction 100
g_maxlives 0
g_minGameClients 8
g_needpass 0
g_redlimbotime 15000
g_voteFlags 0
game jaymod
gamename et
gametype 2
gq_address 78.46.59.14
gq_dedicated
gq_gametype 2
gq_hostname ^3BEGINNERS XP SAVE - STOCK MAPS
gq_mapname goldrush
gq_maxplayers 64
gq_mod jaymod
gq_numplayers 62
gq_online 1
gq_password 0
gq_port 27980
gq_prot quake3
gq_type quake3
hostname ^3BEGINNERS XP SAVE - STOCK MAPS
mapname goldrush
maxlives 0
mod_binary linux-release
mod_url http://jaymod.clanfu.org
mod_version 2.1.7
needpass 0
omnibot_enable 1
omnibot_playing 0
protocol 82
punkbuster 1
pure 1
serverload 32
sv_allowAnonymous 0
sv_cpu Intel(R) Core(TM) i7 CPU
sv_floodProtect 1
sv_hostname ^3BEGINNERS XP SAVE - STOCK MAPS
sv_maxPing 0
sv_maxRate 45000
sv_maxclients 64
sv_minPing 0
sv_minguidage 0
sv_privateClients 0
sv_punkbuster 1
sv_uptime 05d09h44m
timelimit 30
version ET
voteFlags 507903
weaprestrict 100

19

Saturday, April 9th 2011, 1:37pm

Poste bitte mal den Inhalt von beiden Dateien, wie sie gerade in diesem Moment bei dir rumliegen :)

20

Saturday, April 9th 2011, 7:42pm

sidebar.php

PHP Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<table border="0" cellspacing="0" cellpadding="0"  style="background-color:ffb665">
		<tr>
			<td style="background-image:url(images/sub1/sub_light_orange_top_middle.png);repeat">
				<img src="images/sub1/sub_light_orange_top_left_corner.png">
			</td>
			<td width="95%" style="background-image:url(images/sub1/sub_light_orange_top_middle.png);repeat">
			</td>
 
			<td style="background-image:url(images/sub1/sub_light_orange_top_middle.png);repeat">
				<img src="images/sub1/sub_light_orange_top_right_corner.png">
			</td>
		</tr>
 
 
		<tr>
			<td style="background-image:url(images/sub1/sub_light_orange_middle_left.png);background-repeat: repeat-y">
 
			</td>
 
			<td align="left" valign="top" style="font-family:Verdana">
				<font  style="font-weight:bold;">Server:</font><br>	
				<font size="-1"> 
					<?php
 					include ('gameq/example.php');
					 unset($results['sv_cpu']); 
 						print_results($results);
							?> 
					<br>
					<u>Shockvoice Server:</u> <? include ("include/shockvoice.php"); ?>  
                    <br />
				</font>
 
					<table>
 
						<tr><td><font size="-1"> 3ster</font></td><td><img src="images/w_et_icon.png"></td>					
						<tr><td><font size="-1"> Aro</font></td><td><img src="images/w_et_icon.png"></td>
						<tr><td><font size="-1"> knolle</font></td><td><img src="images/w_et_icon.png"></td>
						<tr><td><font size="-1"> Hooligan</font></td><td><img src="images/w_et_icon.png"><img src="images/dod_s_icon.png"><img src="images/coh_icon.png"></td>
						<tr><td><font size="-1"> Beffo</font></td><td><img src="images/w_et_icon.png"></td>
						<tr><td><font size="-1"> k0ks-kEks</font></td><td><img src="images/w_et_icon.png"><img src="images/dod_s_icon.png"><img src="images/coh_icon.png"></td>
						<tr><td><font size="-1"> germanAngst</font></td><td><img src="images/w_et_icon.png"><img src="images/coh_icon.png"></td>
						<tr><td><font size="-1"> Haudi</font></td><td><img src="images/w_et_icon.png"></td>
						<tr><td><font size="-1"> Prototype</font></td><td><img src="images/w_et_icon.png"></td>
						<tr><td><font size="-1"> Welfe</font></td><td><img src="images/dod_s_icon.png"><img src="images/coh_icon.png"></td>
 
					</table>
			</td>
 
			<td style="background-image:url(images/sub1/sub_light_orange_middle_right.png);background-repeat: repeat-y">
 
			</td>
 
		</tr>
 
 
		<tr>
			<td width="32px" height="32px">
				<img src="images/sub1/sub_light_orange_bottom_left_corner.png">
			</td>
 
			<td width="32px" height="32px" style="background-image:url(images/sub1/sub_light_orange_bottom_middle.png);repeat">
			</td>
 
			<td width="32px" height="32px">
				<img src="images/sub1/sub_light_orange_bottom_right_corner.png">
			</td>
		</tr>
 
	</table>



gameq/example.php

PHP Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
error_reporting(E_ALL);
 
 
require_once 'GameQ.php';
 
 
// Define your servers,
// see list.php for all supported games and identifiers.
$servers = array(
    'server 1' => array('quake3', '78.46.59.14', 27980),
);
 
 
// Call the class, and add your servers.
$gq = new GameQ();
$gq->addServers($servers);
 
 
// You can optionally specify some settings
$gq->setOption('timeout', 900);
 
 
// You can optionally specify some output filters,
// these will be applied to the results obtained.
$gq->setFilter('normalise');
$gq->setFilter('sortplayers', 'gq_ping');
 
// Send requests, and parse the data
$results = $gq->requestData();
 
 
 
 
 
 
// Some functions to print the results
function print_results($results) {
 
    foreach ($results as $id => $data) {
 
        printf("<h2>%s</h2>\n", $id);
        print_table($data);
    }
 
}
 
function print_table($data) {
 
    $gqs = array('gq_online', 'gq_address', 'gq_port', 'gq_prot', 'gq_type');
 
 
    if (!$data['gq_online']) {
        printf("<p>The server did not respond within the specified time.</p>\n");
        return;
    }
 
    print("<table><thead><tr><td>Variable</td><td>Value</td></tr></thead><tbody>\n");
 
    foreach ($data as $key => $val) {
 
        if (is_array($val)) continue;
 
        $cls = empty($cls) ? ' class="uneven"' : '';
 
        if (substr($key, 0, 3) == 'gq_') {
            $kcls = (in_array($key, $gqs)) ? 'always' : 'normalise';
            $key = sprintf("<span class=\"key-%s\">%s</span>", $kcls, $key);
        }
 
        printf("<tr%s><td>%s</td><td>%s</td></tr>\n", $cls, $key, $val);
    }
 
    print("</tbody></table>\n");
 
}
 
 
 
 
 
 
 
 
?>

Similar threads

Social bookmarks