Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9207093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:18:53+00:00 2026-06-18T00:18:53+00:00

Hey guys I need some assistance with a php regex problem. Regex might not

  • 0

Hey guys I need some assistance with a php regex problem. Regex might not even be the best method to use here. I do not know how to use php at all – I just surf around looking at examples and try to paste them together to accomplish what I need done.

My current code looks like this…

// full path to text file
define("TEXT_FILE", "cports.log");
// number of lines to read from the end of file
define("LINES_COUNT", 5);


function read_file($file, $lines) {
    $handle = fopen($file, "r");
    $linecounter = $lines;
    $pos = -2;
    $beginning = false;
    $text = array();
    while ($linecounter > 0) {
        $t = " ";
        while ($t != "\n") {
            if(fseek($handle, $pos, SEEK_END) == -1) {
                $beginning = true; 
                break; 
            }
            $t = fgetc($handle);
            $pos --;
        }
        $linecounter --;
        if ($beginning) {
            rewind($handle);
        }
        $text[$lines-$linecounter-1] = fgets($handle);
        if ($beginning) break;
    }
    fclose ($handle);
    return array_reverse($text);
}

$fsize = round(filesize(TEXT_FILE)/1024/1024,2);

$lines = read_file(TEXT_FILE, LINES_COUNT);
foreach ($lines as $line) {
    echo $line;
}

?>

It basically reads a file named cports.log and tails the last 5 lines. However an example of my log file looks like this…

1/27/2013 7:16:06 PM Added          {92.255.176.84}
1/27/2013 7:16:07 PM Removed        {92.255.176.84}
1/27/2013 7:16:08 PM Added          {176.15.101.53}
1/27/2013 7:16:09 PM Removed        {176.15.101.53}
1/27/2013 7:16:23 PM Added          {98.119.183.235}
1/27/2013 7:16:24 PM Removed        {98.119.183.235}
1/27/2013 7:16:27 PM Added          {37.251.51.9}
1/27/2013 7:16:28 PM Removed        {37.251.51.9}
1/27/2013 7:16:38 PM Added          {92.255.176.84}
1/27/2013 7:16:38 PM Added          {82.112.38.83}
1/27/2013 7:16:39 PM Removed        {92.255.176.84}
1/27/2013 7:16:39 PM Removed        {82.112.38.83}
1/27/2013 7:16:45 PM Added          {74.61.121.147}
1/27/2013 7:16:50 PM Removed        {74.61.121.147}

So the output of my php file is just the last 5 lines of the log file. But what I need done is take each IP nested in the brackets and either store them into an array or a variable to be used in this section of code…

<?php

require_once("geoipcity.inc");

$ip = "8.8.8.8";

$gi = geoip_open("GeoLiteCity.dat", GEOIP_STANDARD);

$record = geoip_record_by_addr($gi, $ip);
    echo "Location One <br>";
    echo "Country: " .$record->country_name . "<br>";
    echo "City: " .$record->city . "<br>";
    echo "Latitude: " .$record->latitude . "<br>";
    echo "Longitude: " .$record->longitude . "<br>";

geoip_close($gi);
?>

This block of code is basically iterated 5 times to print out the geolocation of 5 IPs from whatever $ip is. Any help would be much appreciated! Once again I don’t know php so please be patient with me. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:18:55+00:00Added an answer on June 18, 2026 at 12:18 am

    First tip would be using the file() function to read a file line-wise, then you can just iterate over them:

    foreach (file("text.txt") as $line) {
        ###
    }
    

    But using a regex you could read the file at once and have preg_match_all extract all IPs in one swoop:

    preg_match_all('/\{([\d.]+)\}/', file_get_contents($fn), $matches);
    print_r($matches[1]);
    

    With a nicely structured file like that, you can even completely arraytize all columns at once, using

    #^  ([\d/]+)\s  ([\d:]+)\s \w+\s  (.*?)\s  \{([\d.]+)\}\s*  $#mix
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys. I'm not much of a programmer, but still need to do some
Hey guys need some help on the html and css float stuff. Please check
Hey guys I need help on passing multiple values for my PHP http://s596.beta.photobucket.com/user/kingbookal/media/Capture.png.html?sort=3&o=0 That's
Hey guys! I need some help writing a code that creates an array in
Hey guys. I need to compile some project. I installed Visual C++ 6.0 +
Hey guys, I need to make a list of some functions that are listed
Hey guys, i'm having some trouble with this: In the object consumer i need
hey guys, i know how to create a simple php file that mails some
Hey guys need some more help I have 3 tables USERS, PROFILEINTERESTS and INTERESTS
Hey guys i need some help with logic in my app. I have an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.