-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 \
?
It is worth noting that it would be much better to use str_replace which would have none of these problems, besides more efficient.
– Bacco
True, simple and practical!
– Rodrigo Carvalho de Brito
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"
– Victor Padovan
@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 charactersabc
, 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
@hkotsubo Hmmmmm, etendi, thank you very much
– Victor Padovan
@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– hkotsubo