Jump to content

Need help with an array


michael.davis

Recommended Posts

Hi everyone!  Thank you for taking the time to review and help out with this.

 

What I am trying to do, as I am a newby, is take a file called OfficeLine break it down into an array so I an parse the data out of it.  I have the code written and seems to work ok, but I am having issues with the NAME field as it is not a standard layout.  Here is an example of the file OfficeLine:

 

Aug  3 08:14:20 (Name=, Number=6157707790)  [Time: 13:14:18]

Aug  3 13:42:27 (Name=TN NATL GUARD  , Number=6155552121)  [Time: 18:42:23]

Aug  3 13:44:21 (Name=MAST HENRY A  A , Number=6153455432)  [Time: 18:44:19]

Aug  3 13:52:30 (Name=BELL SOUTH    , Number=6156677890)  [Time: 18:52:28]

 

 

As you can see the Name field can be nothing to several words/characters.  Does anyone have any suggestions to look for the variety in this field?

 

My php code is listed below.

 

<?php
$calllog=file('CallLog/OfficeLine');
arsort($calllog);

foreach ($calllog as $callLine){
   echo $callLine;
   echo "<br>";
   $doublespacetocomma = str_replace("  ", ",", $callLine);
   $singlespacetocomma = str_replace(" ", ",", $doublespacetocomma);
   $removeendbracket = str_replace(")", "", $singlespacetocomma);
   $noname = str_replace("(Name=", "", $removeendbracket);
   $nonumber = str_replace("Number=", "", $noname);
   $nogmttime = str_replace("[Time:,", "", $nonumber);
   $nodoublecommas = str_replace(",,", ",", $nogmttime);
   $removebracketend = str_replace("]", "", $nodoublecommas);
   $doubletothreecommas = str_replace(",,", ",,,", $removebracketend);
   $usgovt1 = str_replace(",US,GOVERNMENT,", ",US,Govt,", $doubletothreecommas);
   $usgovt2 = str_replace("UNITED,STATES,G", "US,Govt", $usgovt1);      
   $crazyusgovt = str_replace(",U,S,GOVERNMENT,,,", ",US,Govt,", $usgovt2);
   $stthomas = str_replace(",ST,THOMAS,HOSPI,", ",STTHOMAS,HOSPITAL,", $crazyusgovt);
   $final = str_replace(",TN,ST,GOVT,,,,", ",STOFTN,GOVT,", $stthomas);
   echo $final;
   
   $items=explode(",", $final);
      
      echo "<br>";
      echo "$items[0] $items[1] $items[2] $items[3]  $items[4]  $items[5]" ;
      echo "<br>";
      echo "===================================<br>";
       
}
?>

Thanks!

Mike

 

MOD EDIT: code tags added.

Link to comment
Share on other sites

This is a good time to use regular expressions.

// inside the loop, instead of what you have now
if (preg_match('/([^(]+)\s\(Name=(.*?), Number=(\d+)\)\s\[Time: ([\d:]+)\]/', $callLine, $matches)) {
    print_r($matches);
    // do something with $matches
}

Depending on the size of the file, a preg_match_all() instead of a file()+loop might be better.

Link to comment
Share on other sites

Thank you for the reply. 

 

Adjusted my php script to reflect your suggestions, and I get the exact same output as the input displayed. 

What am I missing here?  :confused: 

 

What I am trying to do is break this list out into individual items so I can match the number with our listing of numbers if they exist.  My issue is the different legth of strings for the Name= area.

 

Thank you again for your and everyone elses help.

 

Mike

Link to comment
Share on other sites

Just a thought, probably a bit crude for the php purists around  :P

 

Why not explode the row by comma to break off the latter part of the row, then explode the result by open bracket to remove the first bit.

 

Then you'll be left with NAME=........................

 

You could I suppose then explode again on the = to be left with just the name.  Seem more straight forward than trying to preg_match a non standard.

Link to comment
Share on other sites

Here is what I came up with...thanks to everyone's help:

<?php
$calllog=file('CallLog/OfficeLine');
arsort($calllog);

foreach ($calllog as $callLine){
   echo $callLine;
   echo "<br>";
   $nonumber = str_replace (" Number=", "", $callLine);
   $prefinal = str_replace(") ", ",", $nonumber);
   $final = str_replace(" (Name=", ",", $prefinal);
   echo $final;
   
   $items=explode(",", $final);
     
      echo "<br>";
      echo "$items[0] $items[1] $items[2]" ;
      echo "<br>";
      echo "===================================<br>";

}
?>

This is the final output:

 

Aug 4 08:58:00 JACK MACDONALD 5148338337

 

Thus $item[0]          $item[1]            $item[2]

 

==================================================================

 

Now I after achieving this, I am trying to compare the $item[2] to a db.txt file that contains a list of people and their numbers to see if we have a match and display this match.  If no match, then just show what is already there.

 

Here is what I tried to add to the existing php script:

<?php
$calllog=file('CallLog/OfficeLine');
arsort($calllog);

foreach ($calllog as $callLine){
   echo $callLine;
   echo "<br>";
   $nonumber = str_replace (" Number=", "", $callLine);
   $prefinal = str_replace(") ", ",", $nonumber);
   $final = str_replace(" (Name=", ",", $prefinal);
   echo $final;
   
   $items=explode(",", $final);
     
      echo "<br>";
      echo "$items[0] $items[1] $items[2]" ;
      echo "<br>";
      echo "===================================<br>";



$database=file('db.txt');
arsort($database);

foreach ($database as $db){
   echo $db;
   echo "<br>";
     
   $s=explode(",", $db);
     
      echo "<br>";
      echo "$s[1]";
      echo "<br>";
      echo "++++++++++++++++++++++++++++++++++++++++<br>";

       if ($items[2] == $s[1]) {
       echo "<font face=Arial, Helvetica, sans-serif size=2 color=red>";
      echo $items[2];
      echo "</font>";
      } else {
      echo "false";
      }
}
}
?>

With this I get no matches even though both files have the exact same numbers inside seperated by commas  I do get a return of FALSE:

 

Here is the db.txt contents:

 

JACK MACDONALD,5148338337

ARNOLD JAMES,6159554406

TN ST GOVT,6157438000

TN ST GOVT,6157437777

 

Thoughts and suggestions?  :shrug:

 

Thanks!

Mike

 

MOD EDIT: code tags added.

Link to comment
Share on other sites

How about something along these lines?

 

//get the file contents
$calllog=file('CallLog/OfficeLine');

//sort the file
arsort($calllog);

//initialise a variable to hold the database file contents
$storage = array();

//get text file contents
$database = file('db.txt');
//sort the file
arsort($database);

foreach($database as $line) {
//make an array of the values
$row = explode(',', $line);
//add the data to the storage variable
$storage[(int)$row[1]] = $row[0];
}

foreach($calllog as $callLine) {
//initialise the storage variable
$matches = array();
//find the name and number
preg_match_all('/(?<=Name\=).*?(?=,)|(?<=Number\=)[0-9].*?(?=\))/', $callLine, $matches);

//make sure we remove any trailing spaces
$name = trim($matches[0][0]);
//cast the number to an int
$number = (int)$matches[0][1];
        
        //if the number exists in the storage variable and the name is also the same -> we have a match
if(in_array($number, array_keys($storage)) && $name == $storage[$number]) {
	echo "<font face=Arial, Helvetica, sans-serif size=2 color=red>";
	echo $number;
	echo "</font>";
}  else {
	echo "false";
}
}

Link to comment
Share on other sites

  • 4 weeks later...

Hi All!

 

This is what I have so far.  The database text file has been changed somewhat, so there is an issue, and I am not sure how to fix it.

 

The db.txt file is in this format now:

 

,,TERRY,BIKER,500 Station Road,Old Hickory,TN,37138,WILSON,6157542111,6157588311 and ,,KERI,SMITH,300 Station Road,Old Hickory,TN,37138,WILSON,615-754-0987,615-758-7890

 

* Note the two different phone number formats. This is an issue too.  :-\

 

Can someone help me tweak the code to reflect this change in the db.txt in my code? ;D

 

What I am trying to accomplish is that if the number matches either fields 10 or 11, then display the data from the db.txt rather than the OfficeLine data. If the numbers to not match in either field, then display the OfficeLine information.

 

Thanks in advance! I appreciate everyone's time and assistance!

 

<?php
//get the file contents
$calllog=file('CallLog/OfficeLine');

//sort the file
arsort($calllog);

//initialise a variable to hold the database file contents
$storage = array();

//get text file contents
$database = file('db.txt');
//sort the file
arsort($database);

foreach($database as $line) {
//make an array of the values
$row = explode(',', $line);
//add the data to the storage variable
$storage[(int)$row[1]] = $row[0];
//$dbnumber = (int)$row[1];
//$dbname = $row[0];
//echo $dbname;
//echo "<br>";
}

foreach($calllog as $callLine) {
//initialise the storage variable
$matches = array();
//find the name and number
preg_match_all('/(?<=Name\=).*?(?=,)|(?<=Number\=)[0-9].*?(?=\))/', $callLine, $matches);

//make sure we remove any trailing spaces
$name = trim($matches[0][0]);
//cast the number to an int
$number = (int)$matches[0][1];
    //echo $name;
//echo $number;
        //if the number exists in the storage variable and the name is also the same -> we have a match
if(in_array($number, array_keys($storage))) {
								//if(in_array($number, array_keys($storage)) && $name == $storage[$number]) {
	echo "<font face=Arial, Helvetica, sans-serif size=2 color=red>";
	echo $name;
	echo "<br>";
	echo $number;
	echo "<br>";
	echo "======================";
	echo "<br>";
	echo "</font>";
}  else {
    echo "<font face=Arial, Helvetica, sans-serif size=2>";
	echo $name;
	echo "<br>";
	echo $number;
	echo "<br>";
	echo "======================";
	echo "<br>";
	echo "</font>";
}
}
?>

Link to comment
Share on other sites

Also one more thing.    The script gathers the name and the number, but I also need to display the data and time at the front of each line.

 

--- OfficeLine File ---

 

Aug  3 08:14:20 (Name=, Number=6157707790)  [Time: 13:14:18]

Aug  3 13:42:27 (Name=TN NATL GUARD  , Number=6155552121)  [Time: 18:42:23]

Aug  3 13:44:21 (Name=MAST HENRY A  A , Number=6153455432)  [Time: 18:44:19]

Aug  3 13:52:30 (Name=BELL SOUTH    , Number=6156677890)  [Time: 18:52:28]

 

--- db.txt file ---

 

,,TERRY,BIKER,500 Station Road,Old Hickory,TN,37138,WILSON,6157542111,6157588311

,,KERI,SMITH,300 Station Road,Old Hickory,TN,37138,WILSON,615-754-0987,615-758-7890

 

So I am need of help for the following:

  • Data/Time
  • Grabbing the line from the db.txt file and breaking it up into different fields to display once the numbers match

 

Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.