preg_replace with result of function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

preg_replace with result of function

Post by vigge89 »

Code: Select all

<?php
##### function for checking if server is online
function server_check ($server) {

	if ($fp = @fsockopen ($server)) { // succesful
		$result = 'Online';
		@fclose ($fp);
	} else { // failed
		$result = 'Offline';
	}
	
	return $result;

} ## /function for checking if server is online


####### parse template
	$pattern = "#\{%server=(.*?)%\}#is";
	$output['body'] = preg_replace ($pattern, server_check ('\\1'), $output['body']);
?>
how can i replace the pattern with the result of the server_check()-function?
the function should be called with the result of the preg_replace.

also, how can i make this "ping"-script work?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

preg_replace('#blah#e','''blahblah'' . highlight_string(''\\1'')',$foo);
although you might like [php_man]preg_replace_callback[/php_man] more..
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

so, something like this:

Code: Select all

<?php
	$output['body'] = preg_replace_callback ($pattern, server_check ('$matches'), $output['body']);
?>
im afraid it doesn't work, now the string doesn't get replaced at all :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$output['body'] = preg_replace_callback( $pattern, 'server_check', $output['body']);
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

great, now I just need to fix the "pinger" :)
any advices regarding it?
it just outputs Offline even if my computer is online :(

Code: Select all

<?php
##### function for checking if server is online
function server_check ($server) {

	if ($fp = @fsockopen ($server[1])) { // succesful
		$result = 'Online';
		@fclose ($fp);
	} else { // failed
		$result = 'Offline';
	}
	
	return $result;

} ## /function for checking if server is online


####### parse output
	$pattern = "#\{%server=(.*?)%\}#is";
	$output['body'] = preg_replace_callback ($pattern, 'server_check', $output['body']);
?>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Might want to look at http://pear.php.net/package/Net_Ping and see how that does it? ;)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

looks rather not-cross-os-available :p
are there any other ways of checking if an server/ip is onlinem while sending and recieving as little data as possible?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if you want to see if http is running.. ask curl to just return the header.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

i don't really get CURL, how does it work? seems rather complicated ><
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

looks interesting, but what does all those setopt('') things do, and how should i set it up too make it only look if the server's up?

sorry for my n00bish questions, put i can't figure out how it works ><
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The setopt calls set options :arrow: [php_man]curl_setopt[/php_man]()...

curl_exec will return with a failure of some sort if the server isn't up.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

i solved it by using:

Code: Select all

<?php
##### function for checking if server is online
function server_check ($server) {

	$fp = @fsockopen ($server[1], 80);
	if ($fp) { // succesful
		$result = 'Online';
		@fclose ($fp);
	} else { // failed
		$result = 'Offline';
	}

	return $result;

} ## /function for checking if server is online


####### parse output
	$pattern = "#\{%server=(.*?)%\}#is";
	$output['body'] = preg_replace_callback ($pattern, 'server_check', $output['body']);
?>
Thanks for the replies feyd :)
Post Reply