.txt import for MySQL - values between strings

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, eg Cliente: FULANO DE TAL CPF: I need it to be between Cliente: e CPF: .

In other parts of the same document I need to get 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, since there may or may not be data between these strings.
Note that I do not have a standard separator character to generate my arrays, I could use : , however some data is not after : but in the line below or even after other data in the line below.

I know it sounds kind of confusing, but what I already got was the following: I found a function on the internet that allows me to get 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);  
}

Even though it is functional, however, in echo , the other data in the document still appears, excluding only the string that I set as the initial and final string.

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

    
asked by anonymous 15.06.2014 / 20:47

2 answers

4

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

Here's 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 search for all occurrences and generate an array with what you find. You just have to define the fields.

Some relevant regex parameters:

() - imdica a capture, which will be collected by php
.* - anything but line break
\s - line break
\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 php's preg_match_all function gave me this: / p>

{
[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) " "
    }
}
    
15.06.2014 / 22:34
1

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

    
15.06.2014 / 21:31