String-how to intercept the IP address in the string "xxx_xxxx_IP address _ xxxxxxxx_xxxxxx" in php?

Source: Internet
Author: User
Tags preg
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.