How can I extract a character from a string in PHP?

Asked

Viewed 232 times

1

I have this code in PHP that takes information from a text file, this information that comes from this file, comes kind in columns delimited by | as you can see I did a explode in | the result came the whole column because the file information came like this:

Example:

| campo1   | nivel  | campo3        | campo4     |
| material |   1    | componente    | quantidade |
| material |   2    | componente2   | quantidade |

The result in the explosion comes out like this: Example:

0=> material material material... até a ultima linha
mesmo coisa para as outras colunas
1=> 1 2 2 2 1 3 3 ... até a ultima linha do arquivo texto.
No nível 1 ele é pai do 2 e o 2 é pai no 3 e assim sucessivamente.

That is, I need to take this table and play in the database so with the column2_pai:

| coluna1  | nível |  coluna2_pai  | coluna2      | coluna3 |
| material |   1   |  componente1  | componente1  |  tipo   |
| material |   2   |  componente1  | componente2  |  tipo   |
| material |   3   |  componente2  | componente3  |  tipo   |
| material |   1   | componente1.1 | componente1.1|  tipo   |
| material |   2   | componente1.1 | componente1.2|  tipo   |

Explaining what I want to do in the 2_parent column will receive the parent level components for each child level. Level 1 is the maximum level has no father your child will always be level 2 below him.

Example:

nivel 
1
2
2
2
1
2

So in this case the first level 1 will only be parent of levels 2 up to the next level 1 the same thing serves for the next levels 1 and level 2 for the 3 and so on.

$material = fopen("c:/inetpub/wwwroot/material1.txt","r");

//$materiais = file("material1.txt");

while(!feof($material)){

    $linha = fgets($material,1024);

    $valor = explode('|',$linha);

    $nivel = $valor[3];

    $component = $valor[4];

    $array_nivel[$nivel] = $component;

    $nivel_pai = $nivel - 1;

    //$array_nivel[0] = $valor[1];

    //echo $valor[3]."\n";

    echo "Nivel ".$nivel_pai." Pai: ".$array_nivel[$nivel]." Filho: ".$component."\n";
    }

The part in $nivel_pai = $nivel - 1 i tried to use the logic of taking the level value for example 2 and subtract by 1 to catch the component in the previous level in the level 2 case will get the component of 1.

the string would be the column itself in the case of the level column I would need to make an if of the values of level 1 2 and 3 just as they are in the same string I’m having trouble making this condition, the condition

if($nivel == 1){
   $array_nivel[$nivel_pai] = $component;
}elseif($nivel == 2){
   $array_nivel[$nivel_pai] = $component;
   $nivel_pai = $nivel - 1;
}... e por ia vai

já tentei if($nivel <> 1 || $nivel == 2 ...){
  $array_nivel[$nivel_pai] == $component;
  $nivel_pai =  $nivel - 1;
}

it does not work the idea is to compare these levels, and take the component of level 1 and place of level 2 2_parent the component of level 2 will already be put in level 3 2_parent column and so on so that these levels are interspersed

level
1
2
2
2
1
2
2
3
2
3
4
4
5
3
2
1
2
2
3
2

  • I think it has some functions in php that does this: scanf

  • Hello, I think I got the logic. I didn’t get the column 2_pai of 1.1 and 1.2. Can you give more examples of the column please

  • What you want is a substring of a string ?

  • It would not be simpler to put everything in an array of arrays and then get what you needed. So you are complicating things without being necessary.

1 answer

1

From what I understand you want to pick up each separate line? If it is, you should rather blow up the line break. If this is the problem, do so so:

<?php

$a = '
| campo1   | nivel  | campo3        | campo4     |
| material |   1    | componente    | quantidade |
| material |   2    | componente2   | quantidade |
';
$a = trim($a); // limpa os espaços em branco

$a = explode("\n", $a);
$i = 0;
foreach($a as $v) {
    $v = trim($v);
    if(!empty($v)) {
        $b = explode("|", $v);
        foreach($b as $nv) {
            $nv = trim($nv);
            if(!empty($nv)) {
                // exit(var_dump($nv));
                $dados[$i][] = $nv;
            }
        }
    }
    $i++;
}

And the remaining result in the variable $dice is the array below:

Array
(
    [0] => Array
        (
            [0] => campo1
            [1] => nivel
            [2] => campo3
            [3] => campo4
        )

    [1] => Array
        (
            [0] => material
            [1] => 1
            [2] => componente
            [3] => quantidade
        )

    [2] => Array
        (
            [0] => material
            [1] => 2
            [2] => componente2
            [3] => quantidade
        )

)

If that’s your question, I hope I helped. Abs.

  • I think you do not understand, the author was very clear, he wants to separate by levels. I hope you understand as a constructive criticism to improve your response.

Browser other questions tagged

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