Page 1 of 1

preg_replace with result of function

Posted: Sat Aug 21, 2004 5:13 am
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?

Posted: Sat Aug 21, 2004 10:16 am
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..

Posted: Sat Aug 21, 2004 11:12 am
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 :?

Posted: Sat Aug 21, 2004 11:19 am
by feyd

Code: Select all

$output['body'] = preg_replace_callback( $pattern, 'server_check', $output['body']);

Posted: Sat Aug 21, 2004 11:20 am
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']);
?>

Posted: Sat Aug 21, 2004 11:35 am
by markl999
Might want to look at http://pear.php.net/package/Net_Ping and see how that does it? ;)

Posted: Sat Aug 21, 2004 3:22 pm
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?

Posted: Sat Aug 21, 2004 3:26 pm
by feyd
if you want to see if http is running.. ask curl to just return the header.

Posted: Sat Aug 21, 2004 3:49 pm
by vigge89
i don't really get CURL, how does it work? seems rather complicated ><

Posted: Sat Aug 21, 2004 3:55 pm
by feyd

Posted: Sat Aug 21, 2004 4:21 pm
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 ><

Posted: Sat Aug 21, 2004 5:05 pm
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.

Posted: Sun Aug 22, 2004 3:16 am
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 :)