In PHP, How can I obtain the "10.136.136.132.61" part of a string such as "xxx_xxxx_10.136.132.61. Because the IP address length is different, the substr I first thought is not applicable. If you look for the location of "_", because the IP address is both... in PHP, a string such as "xxx_xxxx_10.136.132.61_xxxxxx_xxxxxx", how can we get the part "10.136.132.61.
Because the IP address length is different, the substr I first thought is not applicable. If you are looking for the "_" location, because there are two "_" before and after the IP address, so it does not work.
Except for the uncertain length of the IP address, the other values are fixed and the format is also determined. They are all in the format of "xxx_xxxx_IP address _ xxxxxxxx_xxxxxx". x indicates a character.
Thank you for your help!
Reply content:
In PHP, How can I obtain the "10.136.136.132.61" part of a string such as "xxx_xxxx_10.136.132.61.
Because the IP address length is different, the substr I first thought is not applicable. If you are looking for the "_" location, because there are two "_" before and after the IP address, so it does not work.
Except for the uncertain length of the IP address, the other values are fixed and the format is also determined. They are all in the format of "xxx_xxxx_IP address _ xxxxxxxx_xxxxxx". x indicates a character.
Thank you for your help!
If you are sure that other strings do not have the IP-like format, they can be matched.
$str = 'xxx_xxxx_10.136.132.61_xxxxxxxx_xxxxxx';$patten = '(/25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})){3}/';preg_match($patten,$str,$result);var_dump($result);$ip = $result[0];
I don't understand php, But I can answer this question. I. Regular Expression matching. 2. String cutting: Split the head and tail.
Use Regular Expressions
$data = 'xxx_xxxx_10.136.132.61_xxxxxxxx_xxxxxx';$preg = '/[\d\.]+/i';preg_match($preg, $data, $matches);var_dump($matches);
Use the explode function.
$str = 'xxx_xxxx_10.136.132.61_xxxxxxxx_xxxxxx';$arr = explode('_', $str);print_r($arr);
Substr ($ str, 9,-16)
explode('_', $str)[2]
Regular Expressions can be very flexible, but using explode or substr is opportunistic. 3L is positive
Explode is supported, which is easy to use and the format is fixed.