Mysql import . txt - string values

Asked

Viewed 384 times

2

I have a TXT file that contains several different records, in some cases I need to get the information that is between 2 strings, e.g.: Cliente: FULANO DE TAL CPF: I need what this between Cliente: e CPF:.

In other parts of the same document I need to take the first 12 characters of a line after the string Tipo_OS: if there is any information after the Tipo_OS: and before another string also fixed in the document, as there may or may not be data between these strings.
obs. I don’t have a default separator character to generate my arrays, I could use :, but some data are not after the : and yes at the line below or after other data from the line below.

I know it sounds a little confusing, but what I’ve accomplished is this: found a function on the internet that allows me to pick values between 2 strings.

function GetBetween($var1="",$var2="",$pool){

    $temp1 = strpos($pool,$var1)+strlen($var1);
    $result = substr($pool,$temp1,strlen($pool));  
    $dd=strpos($result,$var2);        
    if($dd == 0){ 
        $dd = strlen($result);    
    }    
    return substr($result,0,$dd);  
}

It is even functional, but in the echo continues to appear the other document data excluding the display only the string you set as initial and final.

Initially what I need would be the following, find a value between 2 strings and display only it, but I want to define several values between 2 strings until capturing all information of this service order, after I capture everything I need to run a while to check if there are more things in the next pages, thus assembling my database with the orders of services contained in this document. Every time the document reads Cliente: he identify that is the beginning of a new record.

  • You can put an example of the file .txt?

  • In this model below are 3 Service Order.

  • It exceeded the size, I will put only 2 records

  • I’ll upload the file... I can’t paste the whole file.. exceeds the size

  • Follow . txt file link in Mediafire

  • https://www.mediafire.com/? 1pvi6p2ag92s7o

  • Did Visualize Sergio?

  • You know why they created xml and json ?

  • So Edilson, this question was a little pointless... If you have something to add to help a brother in trouble I am grateful!! Does this type of comment help at all... Is there anything I can do with Json or XML that will help me in my conversion from TXT to MYSQL??? Thanks for the help !!!

Show 4 more comments

2 answers

4


This will give you some work and ideally should be already in a database.

Here is a help using Regex. Regular expressions can be very useful in this type of situation. Take a look at this regex I made for your case.

An online example here (click 'execute code')

In the background you will find all the occurrences and generate an array with what you find. You just have to clear the fields.

Some relevant regex parameters:

() - imdica a capture, which will be harvested by php
.* - anything but breaking lines
\s - line breaking
\s{1,} - one or more line breaks

So this regex,

$regex = '/Cliente:(.*)CPF\/C\.N\.P\.J\.:(.*)\sEndereço:(.*)CEP:(.*)\s{1,}Tel EBT:\s{1,}Tel Res:(.*)Tel Outros:(.*)Tel Comercial:(.*)/';
preg_match_all($regex, $txt, $dados);
var_dump($dados);

with the function preg_match_all php gave me this:

{
[0] => array(
    3
)
    {
    [0] => string(210) "Cliente: FULANO DE TAL CPF/C.N.P.J.: 123456789123 Endereço: R RITA ALVES PEREIRA , 01 -CARAPICUIBA -CARAPICUIBA-SP CEP: 6365000 Tel EBT: Tel Res: 01141874187 Tel Outros: Tel Comercial: 011999999999 "[1] => string(205) "Cliente: OUTRO FULANO DE TAL CPF/C.N.P.J.: 00000000011 Endereço: R ANTONIO FL, 01 -VILA DIRCE -CARAPICUIBA-SP CEP: 6343000 Tel EBT: Tel Res: 01141464146 Tel Outros: Tel Comercial: 011999999999 "[2] => string(192) "Cliente: MAIS UM FULANO DE TAL CPF/C.N.P.J.: 00000000011 Endereço: R TIBIRICA, 00 -VILA DIRCE -CARAPICUIBA-SP CEP: 6335000 Tel EBT: Tel Res: 01141674167 Tel Outros: Tel Comercial: "
    }

[1] => array(
    3
)
    {
    [0] => string(15) " FULANO DE TAL "[1] => string(21) " OUTRO FULANO DE TAL "[2] => string(23) " MAIS UM FULANO DE TAL "
    }

[2] => array(
    3
)
    {
    [0] => string(15) " 123456789123 "[1] => string(14) " 00000000011 "[2] => string(14) " 00000000011 "
    }

[3] => array(
    3
)
    {
    [0] => string(56) " R RITA ALVES PEREIRA , 01 -CARAPICUIBA -CARAPICUIBA-SP "[1] => string(46) " R ANTONIO FL, 01 -VILA DIRCE -CARAPICUIBA-SP "[2] => string(44) " R TIBIRICA, 00 -VILA DIRCE -CARAPICUIBA-SP "
    }

[4] => array(
    3
)
    {
    [0] => string(10) " 6365000 "[1] => string(10) " 6343000 "[2] => string(10) " 6335000 "
    }

[5] => array(
    3
)
    {
    [0] => string(13) " 01141874187 "[1] => string(13) " 01141464146 "[2] => string(13) " 01141674167 "
    }

[6] => array(
    3
)
    {
    [0] => string(1) " "[1] => string(1) " "[2] => string(1) " "
    }

[7] => array(
    3
)
    {
    [0] => string(15) " 011999999999 "[1] => string(15) " 011999999999 "[2] => string(2) " "
    }
}
  • Thank you for the answer, I will do some tests, in relation to should already be in a database, actually it is, but the only way to extract and import such data from the current database is through a pdf file, pdf that extracts the . txt in question.

  • @Ói racø which database uses?

  • I have no idea, it is a system of Claro, we only have access to export a PDF.

  • My code looks like this, returns null in the browser.. can help me...

  • $txtfile = file_get_contents('rotaclaro.txt'); $regex = 'Client:(.)CPF/C.N.P.J.:(.)\sEddition:(.)ZIP CODE:(.)\s{1,}Tel EBT: s{1,}Tel Res:(.)Tel Outros:(.)Tel Comercial:(.)\Stel Celular:(.)Tel Fax:(.)\sReference(.)\sSolicitation:(.)Issuance:(.)Node:(.)\s{2,}Scheduled for: (.{10}) (.)THE NUMBERS:(.)\sEQUIPE:(.)PARTNER:(.)\s{1,}Proposals:(.)Portability:(.)\s{1,}Contract:(.)Date of Habilitation:(.)\s{1,}(.)\s(.)\s(.)\s(.)\s(.)\s(.)\s(.)\s(.)\s(.)\s(.)\s(.)\s(.)\s(.)'; preg_match_all($regex, $txtfile, $dados); var_dump($dados);

  • @Your problem is now related to encoding acho. Have a look here (http://answall.com/q/19150/129) and test echo mb_detect_encoding($txtfile);

  • Returned the following: UTF-8

Show 2 more comments

1

You could use an XLS file and use a function to get the values between tables, so when you put the value in the XLS, it would automatically be added to MYSQL.

  • The purpose of importing TXT is exactly to create an XLS of the same.

  • But he didn’t describe it.

  • Sorry, I changed my name after the post, I need the information to be displayed correctly so I can import this into mysql, and generate an xls later...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.