Page 1 of 1

Split IP Address into variables

Posted: 12 Apr 2013 12:55
by kiwichick
Hi there, Can someone please tell me how I can copy an IP Address to the clipboard then have a script split it into variables?

So:
192.168.2.4

Becomes:
$a = 192
$b = 168
$c = 2
$d = 4

I'm pretty sure I need regexreplace but I don't know how to get the separate sections of the IP Address. Cheers.

Re: Split IP Address into variables

Posted: 12 Apr 2013 13:05
by highend
What exactly do you want to do with these 4 single values afterwards?

There are many ways to get them into variables / do anything with them...

One possibility (assuming you've pasted an ip address to the clipboard before):

Code: Select all

$cb = "<clipboard>";
	$a = gettoken($cb, 1, ".");
	$b = gettoken($cb, 2, ".");
	$c = gettoken($cb, 3, ".");
	$d = gettoken($cb, 4, ".");
Depending on what you really want to do, regexmatches() is also a possible alternative.

Re: Split IP Address into variables

Posted: 12 Apr 2013 13:30
by kiwichick
Thank you so much highend!! I never would have got there!!