• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Help with Regulation REGEX

jetalomar

New Member
Can someone help me with a reg exp. I have a string that looks like this.

"Host hostname IPAddress appers to be up."

I want to write a regular expresation that would take the hostname and ipaddress out of the string and delete the rest. How can I go about writing an expression that does that.
 
use the str_replace() in PHP or ereg_replace(), i'm not sure what one would be better to use.
 
PHP:
<?php
function get_hostname_address( $string, $strict = false )
{
	if( preg_match( 
		$strict ? 
			"~Host ([a-z\.]+) (([0-9]{1,3})\.([0-9]{1,3}+)\.([0-9]{1,3}+)\.([0-9]{1,3}+)) appers to be up~" : 
			"~Host (.[^ ]+) (.[^ ]+) appers to be up\.~si",
		$string,
		$address ) )
	{
		if( array_shift( $address ) )
		{
			return $address ;
		}
	}
}
/** your example **/
echo vsprintf( 
	"Hostname: %s\r\n".
	"Address: %s\r\n", 
	get_hostname_address( "Host hostname IPAddress appers to be up." ) 
);
echo "\r\n";
/** using strict regex might work better **/
echo vsprintf( 
	"Hostname: %s\r\n".
	"Address: %s\r\n", 
	get_hostname_address( "Host domain.com 192.168.0.2 appers to be up.", true ) 
);
?>

Pretty sure you example contains spelling errors, might want to check that and change the regex as necessary ...
 
Thanks for the quick respond krakjoe. I did have some spelling issues.

How do I write that code that you have in the preg_match and how do I understand what it does?
 
Back
Top