-3

I have a IPv6 address with port also [2401:2401:2401:2401:2401:2401:2401:2401]:1234. I want to get the IPv6 address only in PHP. I am trying to do:

$str = "[2401:2401:2401:2401:2401:2401:2401:2401]:1234";
preg_match("[]", $str, $matches);
echo $matches[1];

But this does not get me the proper result. I want matches[1] to contain only "2401:2401:2401:2401:2401:2401:2401:2401".

hwnd
  • 65,661
  • 4
  • 77
  • 114
user1159517
  • 3,374
  • 6
  • 24
  • 42
  • FYI: Paste your code in the question, don't use pastebin (unless the code is *way* too big to fit in the question, in which case, copy a snippet into the question). – Rocket Hazmat Jun 02 '14 at 18:33
  • Not sure why marked this question as duplicate as my question is to know the answer in PHP instead of a regular regex. – user1159517 Jun 02 '14 at 18:44
  • A regex is a regex. The answers from that question should work here, too. – Rocket Hazmat Jun 02 '14 at 18:45

1 Answers1

1

The preg_match you're using won't work. You need something like this:

preg_match("/\[([0-9a-fA-F:]+)\]/", $str, $matches);
Adi Ulici
  • 1,183
  • 7
  • 16