Split IP Address into variables

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
kiwichick
Posts: 677
Joined: 08 Aug 2012 04:14
Location: Windows 10 Pro 22H2, 150% scaling

Split IP Address into variables

Post 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.
Windows 10 Pro 22H2

highend
Posts: 14956
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Split IP Address into variables

Post 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.
One of my scripts helped you out? Please donate via Paypal

kiwichick
Posts: 677
Joined: 08 Aug 2012 04:14
Location: Windows 10 Pro 22H2, 150% scaling

Re: Split IP Address into variables

Post by kiwichick »

Thank you so much highend!! I never would have got there!!
Windows 10 Pro 22H2

Post Reply