Separate values from a string and execute a function with the same

Asked

Viewed 316 times

3

Hello,

I have a string that the value is zero ($contador = 0;), other that the value is three ($vezes = 3;) and another that is $numeros = "13|123456789\n14|123456780\n".

I want PHP to do a function (example echo();) up to the string $contador arrive on three, and do this function with the 2 values in the string $numeros, by default:

$ddd = 13; //Pega o valor 13 da string $numeros
$numero = 123456789; //Pega o valor 123456789 da string $numeros
echo "Meu DDD é $ddd e meu celular é $numero!";
//Resposta (vai repetir a função echo acima 3 vezes graças ao valor da string $vezes):
// Meu DDD é 13 e meu celular é 123456789!
// Meu DDD é 13 e meu celular é 123456789!
// Meu DDD é 13 e meu celular é 123456789!

Same thing with the second string value $numeros. And when the string value $contador reach three, the script to.

I couldn’t think of anything that would do that, thank you all at once!

  • 13|123456789\n14|123456780\n Is that how you have the data? I don’t understand the part where the loop goes into it. It’s always 2 phone numbers?

  • @Papacharlie The data is sent from a POST form, of this format DD|NUMERO, I want a function that every row that user add (\n) script does the function I quoted.

  • You need to format 13|123456789\n14|123456780\n in 13(*)123456789; 14(*)123456780, that?

  • Exactly, but with so much that the formatting does not affect the result with the function, for example echo "Meu DDD é 13.. e meu celular é 123456789"

  • As if it were an array

  • I just read here in php.net, I don’t understand much, so I thought I’d open a question here at stackoverflow

  • I gave an answer based on what I understood of your question, if it is incorrect I edit. See if you solve.

Show 2 more comments

3 answers

2

What you want is to make a loop, there are various and various ways of doing this and various topics about it here.

If the string holds: (DDD)|(NUMERO)\n, each \n you will have a (DDD)|(NUMERO), soon divide the | and will have the (DDD) and the (NUMERO) of each set.

First break the string using explode:

$numeros = "13|123456789\n14|123456780\n";
$numeros = explode("\n", $numeros);

Now you will have an array for each (DDD)|(NUMERO).

Then use can use the for:

for($contador = 0; $contador < 3; $contador++){

    if(isset($numeros[$contador]) && !empty($numeros[$contador])){

     // Use outro explode para dividir entre DDD e NUMERO
     list($ddd, $numero) = explode("|", $numeros[$contador]);
     echo "Meu DDD é $ddd e meu celular é $numero!";
    }

}

You can also use the foreach, to list ALL:

You can also limit using if($index === 3){break;} for example!

foreach($numeros as $index => $numero){

    if(!empty($numero)){
         list($ddd, $numero) = explode("|", $numero);
         echo "Meu DDD é $ddd e meu celular é $numero!";
    }

} 

You can test both by clicking here.

  • But I want to receive string values $ddd and $numero string $numeros

  • Like an array, you know?

  • I edited, see if that’s it. : s

  • You’re almost right, you’re thinking that the $contador is setting as much of time as will make the function echo, the $contador is to repeat as much once the function will occur.

2


Use the explode:

$numeros = "13|123456789\n14|123456780\n";

$contador = 0;
$vezes = 3;

while ($contador < $vezes) {
    $linhas = explode("\n", $numeros);

    foreach($linhas as $linha){
        list($ddd, $numero) = array_pad(explode('|', $linha, 2), 2, null);

        if (isset($ddd, $numero)){
            echo "Meu DDD é $ddd e meu celular é $numero!\n";
        }
        $contador++;
    }

}

// Meu DDD é 13 e meu celular é 123456789!
// Meu DDD é 14 e meu celular é 123456780!

See demonstração

  • There was an error in the demonstration and here. Undefined offset: 1 on line number 11

  • @STRILEXLIVE I already fixed the error, see if this is what you want. I used the variable contador within the foreach not to repeat the same results with each iteration of while, you can put the variable contador outside the foreach if you want.

1

Using preg_match_all to get the numbers only. You will have an array as below.

Array
(
    [0] => Array
        (
            [0] => 13
            [1] => 123456789
            [2] => 14
            [3] => 123456780
        )

    [1] => Array
        (
            [0] => 13
            [1] => 123456789
            [2] => 14
            [3] => 123456780
        )

)

Using the array contents, you can easily join the DDD and phone number.

preg_match_all( '/(\d+)/' , '13|123456789\n14|123456780\n', $fone );

echo $fone[0][0] . '**' . $fone[0][1]; // 13**123456789
echo $fone[0][2] . '**' . $fone[0][3]; // 14**123456789

Browser other questions tagged

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