string-How do I intercept the IP address in the string "Xxx_xxxx_ip address _xxxxxxxx_xxxxxx" in PHP?

Source: Internet
Author: User
Keywords Php string
Tags preg
In PHP, a string such as "xxx_xxxx_10.136.132.61_xxxxxxxx_xxxxxx", How to get the "10.136.132.61" part of it.

Because the IP address is different in length, so I think at the beginning of the substr is not applicable. If you look for "_" position, because there are two "_" before and after the IP, so also not.

In addition to the IP address length is not determined, the other length is determined, the format is also determined, are "XXX_XXXX_IP address _xxxxxxxx_xxxxxx" This format, X represents a character

Thank you all for your help!

Reply content:

In PHP, a string such as "xxx_xxxx_10.136.132.61_xxxxxxxx_xxxxxx", How to get the "10.136.132.61" part of it.

Because the IP address is different in length, so I think at the beginning of the substr is not applicable. If you look for "_" position, because there are two "_" before and after the IP, so also not.

In addition to the IP address length is not determined, the other length is determined, the format is also determined, are "XXX_XXXX_IP address _xxxxxxxx_xxxxxx" This format, X represents a character

Thank you all for your help!

If you can determine that other strings do not have the class IP format, you can match

$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];

Do not understand PHP, but this question can be answered next, one, the regular match. Two, cut the string, cut the tail.

Use the regular bar.

$data = 'xxx_xxxx_10.136.132.61_xxxxxxxx_xxxxxx';$preg = '/[\d\.]+/i';preg_match($preg, $data, $matches);var_dump($matches);

Explode function is done.

$str = 'xxx_xxxx_10.136.132.61_xxxxxxxx_xxxxxx';$arr = explode('_', $str);print_r($arr);

I ask myself to answer, can use substr ($STR, 9,-16)

explode('_', $str)[2]

With the regular can be very flexible, and with explode, or substr, are opportunistic. 3L is the positive solution

Support explode, simple and convenient, anyway 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.