Remove Characters from String

Asked

Viewed 39 times

-2

I have this field below that returns from my API, but as you can see it is with \n, \r and \ amid :na categoria \"A ou B\" I’d like to remove them:

 `"observacao":` `"Conforme Resolução CONTRAN 561/2015.\nÉ Obrigatório relatar a situação observada. \nEx: \"Condutor habilitado na categoria \"A ou B\" conduzindo CVC (Combinação de Veículos de Carga)\".Pode Configurar Crime (Art.309-CTB).\n\nPPD=Permissão Provisória para Dirigir.\nAlterado pela Lei Federal 13.281/2016\n\n\nAlterado pela Lei Federal 13.281/2016\n"`

When you pass str_replace and preg_replace in this way:

$frasesRetiradas = array('Conforme Resolução CONTRAN561/2015.', 'É Obrigatório relatar a situação observada.');
$response['Infracao']['observacao'] = str_replace($frasesRetiradas,'', $response['Infracao']['observacao']);
$response['Infracao']['observacao'] = preg_replace('~[\r\n]+~', '', trim($response['Infracao']['observacao']));

He returned me like this:

    `"observacao":` `"Conforme Resolução CONTRAN 561/2015. Ex: \"Condutor habilitado na categoria \"A ou B\" conduzindo CVC (Combinação de Veículos de Carga)\".Pode Configurar Crime (Art.309-CTB).PPD=Permissão Provisória para Dirigir.Alterado pela Lei Federal 13.281/2016Alterado pela Lei Federal 13.281/2016"`

That is, removed the \n but kept the \ in the string. How do I remove this \?

1 answer

2


To specify a literal backslash, fold it ( ). All other occurrences of the backslash will be treated as a literal backslash: this means that other exhaust sequences you are used to using, such as r or n, will be literally printed instead of having any special meaning.

Extract from the PHP manual available at: https://www.php.net/manual/en/language.types.string.php

$frasesRetiradas = array('Conforme Resolução CONTRAN561/2015.', 'É Obrigatório relatar a situação observada.');
$response['Infracao']['observacao'] = str_replace($frasesRetiradas,'', $response['Infracao']['observacao']);
$response['Infracao']['observacao'] = preg_replace('~[\\r\\n]+~', '', trim($response['Infracao']['observacao']));
  • 2

    It is worth noting that it would be much better to use str_replace which would have none of these problems, besides more efficient.

  • True, simple and practical!

  • I did so but it returns me the string : "According to CONTRAN 561/2015 Resolution. Ex: "Driver enabled in category "A or B" driving CVC (Combination of Cargo Vehicles)". Can Set Up Crime (Art.309-CTB). PPD=Provisional Permission to Drive.Amended by Federal Law 13,281/2016Altered by Federal Law 13,281/2016"

  • 1

    @Victorpadovan If you are returning a JSON, then \" is a valid exhaust string so that the quotes are not confused with the string delimiters (ie, "abc" is a string that only has characters abc, but if I want there to be quotes inside of it, I’d have to put "\"abc\"", to make her "abc"). Then you shouldn’t take the \

  • @hkotsubo Hmmmmm, etendi, thank you very much

  • @Victorpadovan It is worth remembering that in regex it makes no difference to change \r for \\r, even more in single quotes: https://ideone.com/YOrDGR

Show 1 more comment

Browser other questions tagged

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