Array in PHP str_replace

Asked

Viewed 49 times

1

I have stored in a table, a statement template:

I declare for due purpose that _________ enrollment _______ appeared in the company on the day ______ to _______ to provide the service of __________

Only I’m willing to trade ______ for the data that comes from the bank. Here are my frustrated attempts:

Query:

$sql = mysqli_query($this->conexao,"SELECT * FROM....");
$pe = mysqli_fetch_object($sql);

Attempt 1:

$texto = $visualizar->Modelo;
$buscar = array("____","____","____","____","____");
$alterar = array($pe->Nome,$pe->Matricula,date("d/m/Y"),date("H:i"),$pe->Servico);
$incluir = str_replace($buscar,$alterar,$texto);
echo $incluir;

Attempt 2:

$texto = $visualizarD[1]->Modelo;
$buscar = array("____","____","____","____","____");
$alterar = array($pe->Nome,$pe->Matricula,date("d/m/Y"),date("H:i"),$pe->Servico);
$incluir = '';
  foreach($alterar as $altera)
  {
    $incluir .= str_replace($buscar,$altera,$texto);
  }
echo $incluir;

Attempt 3:

$texto = $visualizarD[1]->Modelo;
$buscar = array("____","____","____","____","____");
$alterar = array($pe->Nome,$pe->Matricula,date("d/m/Y"),date("H:i"),$pe->Servico);
$incluir = '';
  for($i = 0; $i <= 3; $i++)
  {
    $incluir .= str_replace($buscar[$i],$altera[$i],$texto);
  }
echo $incluir;

Anyway, they all went wrong. The last 02 repeated the text and in each repeated text, the fields ______ were filled by the names I wanted to change, but each name in a given text.

I would just like to point out that my doubt has nothing to do with my previous post, which I was able to successfully resolve. In that case, I need to do the reverse.

  • Well, there will always be five data to be overwritten?

  • Hello Filipe. This. The 05 data will come from the database.

  • 1

    And that doesn’t solve the problem? https://answall.com/q/360697/5878

  • So as a suggestion, and also for ease of reading, I suggest you replace "_" with or #1, #2, #3... and then just replace the information.

  • 1

    Perfect Philip. You’re right, I switched ___ to #1, #2, #3, #4, #5 and it worked. Thank you very much. If you want to put in answer, I will mark as accepted.

  • Hi Anderson, thanks for the link, but Filipe’s information helped me.

Show 1 more comment

1 answer

1


So as a suggestion, and also for ease of reading, I suggest you replace "_" with or #1, #2, #3... and then just replace the information

Follow the codes with the two examples.

<?php
$texto = "Declaro para devido fins que o #1 matrícula #2 compareceu na empresa no dia #3 às #4 para prestar o serviço de #5";


$textoNovo = str_replace("#1", 'Fulano', $texto);
$textoNovo = str_replace("#2", '012345', $textoNovo);
$textoNovo = str_replace("#3", '10/10', $textoNovo);
$textoNovo = str_replace("#4", '10h', $textoNovo);
$textoNovo = str_replace("#5", 'estudos', $textoNovo);

echo $textoNovo;


$texto = "Declaro para devido fins que o _____ matrícula _____ compareceu na empresa no dia _____ às _____ para prestar o serviço de _____";


$textoNovo = preg_replace("/_____/", 'Fulano', $texto, 1);
$textoNovo = preg_replace("/_____/", '012345', $textoNovo, 1);
$textoNovo = preg_replace("/_____/", '10/10', $textoNovo, 1);
$textoNovo = preg_replace("/_____/", '10h', $textoNovo, 1);
$textoNovo = preg_replace("/_____/", 'estudos', $textoNovo, 1);

echo $textoNovo;
  • 2

    Instead of multiple calls to str_replace, you can use the strtr as I indicated in https://answall.com/q/360697/5878. With it you define a array and replaces everything at once.

  • 1

    Living and learning, thank you. :)

Browser other questions tagged

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