How can I store this log data in database - php

I have a log file from firewall, and I want to store it in mysql database using php, I have used the same key for database, I want to separate the log data in array key=value format and again there is space between each key values, and there are some countries which contain spaces between the coutry name. So how can I complete my task. And for the idea here you can see the log file:
date=2016-04-11 time=11:26:29 logid=0000000013 type=traffic
subtype=forward level=notice vd=root srcip=10.10.24.232 srcport=35321
srcintf="port2" dstip=173.252.74.22 dstport=443 dstintf="wan1"
poluuid=426a22f0-b2d8-51e5-4e06-b3d158ed335f sessionid=11469008
proto=6 action=deny policyid=33 dstcountry="United States"
srccountry="Reserved" trandisp=snat transip=202.166.220.127
transport=35321 service="HTTPS" appid=15832 app="Facebook"
appcat="Social.Media" apprisk=medium applist="GEN-ACC-FBBLK"
appact=drop-session duration=22 sentbyte=120 rcvdbyte=60 sentpkt=2
utmaction=block countapp=1 utmref=62972-2591658 date=2016-04-11
time=11:26:29 logid=0000000013 type=traffic subtype=forward
level=notice vd=root srcip=10.10.37.60 srcport=43857 srcintf="port2"
dstip=202.166.193.187 dstport=443 dstintf="wan1"
poluuid=426a22f0-b2d8-51e5-4e06-b3d158ed335f sessionid=11373387
proto=6 action=close policyid=33 dstcountry="Nepal"
srccountry="Reserved" trandisp=snat transip=202.166.220.127
transport=43857 service="HTTPS" appid=41542 app="SSL_TLSv1.0"
appcat="Network.Service" apprisk=medium applist="GEN-ACC-FBBLK"
appact=detected duration=424 sentbyte=1320 rcvdbyte=1582 sentpkt=10
rcvdpkt=16 utmaction=allow countapp=2 utmref=62972-2591632

Finally I can correct it myself, I hope it will help to others too:
$str = file_get_contents($_FILES["file"]["tmp_name"]);
$str1 = str_replace("\"", "", ($str) );
$vals=split('date=', $str1);
array_shift($vals);
$finalArray = array();
$j = 0;
foreach($vals as $v){
$finalArray[$j]["date"] = substr($v, 0, 10);
//var_dump($v);
$tempString = substr($v, 11);
$tempArr = explode(" ", $tempString);
$prevTemp = "";
foreach($tempArr as $i){
$tmp = explode("=",$i);
if(!isset($tmp[1])){
$finalArray[$j][$prevTemp] = $finalArray[$j][$prevTemp]." ".$i;
}
else{
$finalArray[$j][$tmp[0]] = isset($tmp[1]) ? $tmp[1]: '';
$prevTemp = $tmp[0];
}
}
$j++;
}
//var_dump($finalArray);
//die();
$totalQuery = '';
foreach($finalArray as $val){
if(is_array($val)){
$queryText = 'INSERT INTO `intrulog` SET';
$i = 0;
foreach($val as $k=>$v){
if(isset($v) && !empty($v)){
if($i)
$queryText .= ',';
$queryText .= " `{$k}` = '{$v}' ";
$i++;
}
}
$queryText .= ";<br>";
$totalQuery .= $queryText;
}
}
//echo $queryText;
}

Related

How to check if a word is single or multi and to dived in 2 array in php

I have to check a array of string data .
For example this is the string :
$keywords="today, tomorroy, world, is a good day , a good day is, today"
I have to check if is single word or multi word.
If is multi word i have to order and leave only one for example : is a good day ; a good day is i have to leave only : is a good day . This words i have to store in other array.
In the end i have to have only this results:
This is my code:
$keywords = "";
$singleword="";
$multiword="";
foreach ($response->getResults() as $result) {
$keywords .= $result->getText()->getValue() . ",";
if(count(explode(' ', $keywords)) > 1) {
$multiword++;
}
$singleword++;
}
return $keywordsgenerated;
I need to return : today, tomorroy, world, is a good day
Please can you help me to fix , i'm new in php.
EDIT
I think you were looking for something like this:
<?php
$keywords="today, tomorrow, world, is a good day , a good day is, today";
$arr = array_unique(explode(",",$keywords));
$words = [];
foreach($arr as $key=>$a) {
$words_explode = explode(" ", $a);
foreach($words_explode as $w) {
$words[] = $w;
}
}
$new_words = array_unique($words);
$result = implode(" ", $new_words);
foreach($arr as $item) {
$result = str_replace($item,$item.",",$result);
}
$result = trim($result,", ");
var_dump($result);
UPDATE:
I think? I got what you were looking for?
$keywords = "hansgrohe,hansgrohe focus,küchenarmatur,hansgrohe metris";
$arr = explode(',', $keywords);
$already = [];
$result = '';
foreach($arr as $word) {
$subword = explode(" ", $word);
foreach($subword as $actual_word) {
if (!in_array($actual_word, $already)) {
$result .= $actual_word . ' ';
$already[] = $actual_word;
}
}
}
$result = rtrim($result);
//would result in hansgrohe focus küchenarmatur metris
echo $result;
UPDATE2:
If you want to know number of occurences of each word.
<?php
$keywords = "hansgrohe,hansgrohe focus,küchenarmatur,hansgrohe metris";
$arr = explode(",", $keywords);
$already = [];
$nrwords = [];
$result = '';
foreach($arr as $word) {
$subword = explode(" ", $word);
foreach($subword as $actual_word) {
if (!in_array($actual_word, $already)) {
$result .= $actual_word . ' ';
$already[] = $actual_word;
$nrwords[$actual_word] = 0;
}
$nrwords[$actual_word]++;
}
}
$result = rtrim($result);
//would result in hansgrohe focus küchenarmatur metris
echo $result;
//would show how many of each word that exists
echo '<pre>';
print_r($nrwords);
echo '</pre>';

First word of a comma separated sentence php

My string is : Hi my, name is abc
I would like to output "Hi Name".
[Basically first word of comma separated sentences].
However sometimes my sentence can also be Hi my, "name is, abc"
[If the sentence itself has a comma then the sentence is enclosed with ""].
My output in this case should also be "Hi Name".
So Far I've done this
$str = "hi my,name is abc";
$result = explode(',',$str); //parsing with , as delimiter
foreach ($result as $results) {
$x = explode(' ',$results); // parsing with " " as delimiter
forach($x as $y){}
}
You can use explode to achieve YOUR RESULT and for IGINORE ' OR " use trim
$str = 'hi my,"name is abc"';
$result = explode(',',$str); //parsing with , as delimiter
$first = explode(' ',$result[0]);
$first = $first[0];
$second = explode(' ',$result[1]);
$second = trim($second[0],"'\"");
$op = $first." ".$second;
echo ucwords($op);
EDIT or if you want it for all , separated values use foreach
$str = 'hi my,"name is abc"';
$result = explode(',',$str); //parsing with , as delimiter
$op = "";
foreach($result as $value)
{
$tmp = explode(' ',$value);
$op .= trim($tmp[0],"'\"")." ";
}
$op = rtrim($op);
echo ucwords($op);
Basically it's hard to resolve this issue using explode, str_pos, etc. In this case you should use state machine approach.
<?php
function getFirstWords($str)
{
$state = '';
$parts = [];
$buf = '';
for ($i = 0; $i < strlen($str); $i++) {
$char = $str[$i];
if ($char == '"') {
$state = $state == '' ? '"' : '';
continue;
}
if ($state == '' && $char == ',') {
$_ = explode(' ', trim($buf));
$parts[] = ucfirst(reset($_));
$buf = '';
continue;
}
$buf .= $char;
}
if ($buf != '') {
$_ = explode(' ', trim($buf));
$parts[] = ucfirst(reset($_));
}
return implode(' ', $parts);
}
foreach (['Hi my, "name is, abc"', 'Hi my, name is abc'] as $str) {
echo getFirstWords($str), PHP_EOL;
}
It will output Hi Name twice
Demo

Need to change case of a string - PHP

$variable = "test_company_insurance_llc_chennai_limited_w-8tyu.pdf";
I need to display above the $variable like
Test Company Insurance LLC Chennai Limited W-8TYU.pdf
For that I've done:
$variable = str_replace("_"," ","test_company_insurance_llc_chennai_limited_w-8tyu.pdf");
$test = explode(" ", $variable);
$countof = count($test);
for ($x=0; $x<$countof; $x++) {
if($test[$x] == 'w-8tyu' || $test[$x] == 'llc') {
$test[$x] = strtoupper($test[$x]);
//todo
}
}
I've got stuck in the to-do part.
I will change the specific words to uppercase using strtoupper.
Later, how should I need to merge the array?
Any help will be thankful...
$str_in = "test_company_insurance_llc_chennai_limited_w-8tyu.pdf";
$lst_in = explode("_", $str_in);
$lst_out = array();
foreach ($lst_in as $val) {
switch($val) {
case "llc" : $lst_out[] = strtoupper($val);
break;
case "w-8tyu.pdf" : $lst_temp = explode('.', $val);
$lst_out[] = strtoupper($lst_temp[0]) . "." . $lst_temp[1];
break;
default : $lst_out[] = ucfirst($val);
}
}
$str_out = implode(' ', $lst_out);
echo $str_out;
Not terribly elegant, but perhaps slightly more flexible.
$v = str_replace("_"," ","test_company_insurance_llc_chennai_limited_w-8tyu.pdf");
$acronyms = array('llc', 'w-8tyu');
$ignores = array('pdf');
$v = preg_replace_callback('/(?:[^\._\s]+)/', function ($match) use ($acronyms, $ignores) {
if (in_array($match[0], $ignores)) {
return $match[0];
}
return in_array($match[0], $acronyms) ? strtoupper($match[0]) : ucfirst($match[0]);
}, $v);
echo $v;
The ignores can be removed provided you separate the extension from the initial value.
See the code below. I have printed the output of the code as your expected one. So Run it and reply me...
$variable = str_replace("_"," ","test_company_insurance_llc_chennai_limited_w-8tyu.pdf");
$test = explode(" ", $variable);
$countof = count($test);
for ($x=0; $x<$countof; $x++) {
if($test[$x] == 'llc') {
$test[$x] = strtoupper($test[$x]);
//todo
}elseif($test[$x] == 'w-8tyu.pdf'){
$file=basename($test[$x],'pdf');
$info = new SplFileInfo($test[$x]);
$test[$x] = strtoupper($file).$info->getExtension();
}
else{
$test[$x]=ucfirst($test[$x]);
}
}
echo '<pre>';
print_r($test);
echo '</pre>';
echo $output = implode(" ", $test);

php split mysql field type into parts with preg_replace

I have a string... ENUM('0','1')
and I want to get an array... array('ENUM', array(0, 1));
and I want to do it with preg_replace instead of using multiple complicated explode statements
like so...
$showColumns = 'SHOW COLUMNS FROM table';
if (strpos($show[$k][1], '(')!==FALSE) {
$showType = explode("(", $show[$k][1]);//PREG_SPLIT IS BETTER
}
$elementType = strtoupper($showType[0]);
$type = $this->FIELD_TYPE[$elementType];
$vals = explode(")", $showType[1]);
if (is_integer($vals[0])) {
$values = $vals[0];
}elseif (strpos($vals[0], ',')!==FALSE) {
$vs = explode(',', $vals[0]);
$vs = array_map('str_replace', $vs, array('\'', '', $vs[0]));
$values=array();
foreach ($vs as $v) {
$values[] = $v;
}
}
$values = $vals[0];
print $values; print '<br /><br />';

Replace string in php

I have this string:
525,294,475,215,365,745
and i need to remove 475..and comma.
and if i need to remove first number i need to remove also the next comma, also for last.
I can i do?
a regular expression?
thx
$newStr = str_replace(',475', '525,294,475,215,365,745');
Or the less error prone way:
$new = array();
$pieces = explode(',', '525,294,475,215,365,745');
foreach ($pieces as $piece) {
if ($piece != 475) {
$new[] = $piece;
}
}
$newStr = implode(',', $new);
Here's a regular expression:
$s = "525,294,475,215,365,745";
$s = preg_replace(',?475', '', $s);
$data = "525,294,475,215,365,745";
$parts = explode(',', $data);
for ($i = 0; $i < count($parts); $i++) {
if ($parts[$i] == 475) {
unset($parts[$i]);
}
}
$newdata = join(',', $parts);
<?php
$bigString = "525,294,475,215,365,745";
$pos = strpos($bigString, ",");
while($pos != false) {
$newString .= substr($bigString, 0, $pos);
$bigString = substr($bigString, $pos + 1);
$pos = strpos($bigString, ",");
}
echo $newString;
?>
function filterValue($index, &$a)
{
$key = array_search($index, $a);
if ($key != false) {
unset($a[$key]);
}
}
// Original data
$data = "525,294,475,215,365,745";
$data = explode(',', $data);
filterValue('475', $data);
$output = implode(',', $data);

Categories