Forum Moderators: coopster
I'm trying to get the IP address in a string which might follow one of the three forms:-
foo:trillian@192.168.0.1
foo:trillian@192.168.0.1:8080
foo:trillian@192.168.0.1;unique=asdfghjkl
So the first part of the split will always be on a "@" and it'll end on either EOL or a : or a ;
I got about this far:-
$contactIP = preg_split("/@ and : ¦ ;/", $result->fields[2]);
Which doesn't seem to work at all and array[0] always equals the original string.
I'm sure I've done something stupid, but I'm finding the examples on php.net really hard to follow....
Thanks!
TJ
maybe you should "match" instead of "split":
$needle = "/\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}/";
$haystack = "anything001.00.1.1:anything";
preg_match($needle, $haystack, $result);
if (isset ($result[0])) {
echo "found:" . $result [0];
}
else {
echo "close, but no cigar!";
}
This code-snippet finds any IP address within any string, so you don't have to care about other incoming formats in the future. You might have to adjust the "needle" variable, because e.g. 888.888.888.888 would also pass this test.
Hope this helps,
nerd.