Results 1 to 1 of 1

Thread: Parse all IPs from a string

  1. #1
    Private Lonsofore's Avatar
    Join Date
    Oct 2016
    Posts
    86
    Thanks
    82
    Thanked 38 Times in 25 Posts

    Parse all IPs from a string

    Hello all. Here is a little script to parse all ip from your string. You can use it to your server messages and find some spamers on your servers (who write something like "connect 123.45.6.7! best server ever!").
    Doesn't require libcod.
    Returns undefined if there is no IP or an array with list of them.

    PHP Code:
    getIndexesOfEqual(strchar)
    {
        
    arr = [];
        for (
    i=0i<str.sizei++)
        {
            if (
    str[i] == char)
                
    arr[arr.size] = i;
        }
        return 
    arr;
    }

    parseIPs(str)
    {
        
    arr getIndexesOfEqual(str".");
        
        
    // check, if we have less than 3 dots
        
    if (arr.size 3)
            return 
    undefined;
        
        
    // check the distance between dots (more than 3 is wrong)
        
    dot1 = [];
        
    dot2 = [];
        for (
    i=2i<arr.sizei++)
        {
            if (
    arr[i-2]>&& arr[i]<str.size-&& arr[i]-arr[i-1]<=&& arr[i-1]-arr[i-2]<=3)
            {
                
    dot1[dot1.size] = arr[i-2];
                
    dot2[dot2.size] = arr[i];
            }
        }

        
    // if there is no dots
        
    if (dot1.size == 0)
            return 
    undefined;
            
        
    ips = [];
        for (
    i=0i<dot1.sizei++)
        {
            
    start dot1[i];
            
    end dot2[i];
            
            
    // save old values
            
    oldstart start;
            
    oldend end;
            
            
    // find real start, end
            
    for (j=0j<=2j++)
            {
                if (
    start-1>=&& isInt(str[start-1]))
                    
    start--;
                if (
    end+1<str.size && isInt(str[end+1]))
                    
    end++;
            }
            
            
    // and check, is start/end is a dot
            
    if (start==oldstart || end==oldend)
                continue;
            
            
    // check all other symbols then
            
    for (j=startj<endj++)
            {
                if (
    str[j] != "." && !isInt(str[j]))
                    continue;
            }
            
            
    // save ip
            
    ips[ips.size] = getSubStr(strstartend+1);
        }
        
        if (
    ips.size == 0)
            return 
    undefined;
        else
            return 
    ips;

    Last edited by Lonsofore; 8th July 2017 at 07:51.

  2. The Following User Says Thank You to Lonsofore For This Useful Post:

    kung foo man (8th July 2017)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •