Insert a character in a specific point of a String

Asked

Viewed 59 times

0

I need a light. I have a program that receives, processes a txt and plays on the bench. The txt comes this way:

1;;Company Name 145241;00.000.000/0001-77;A;0,00000000;5000

Each ' ; ' separates the information that is passed to the bank.

The problem is that I only need to get the code that is next to the Company Name.

And I can’t think of any way to insert a ; there separating the company from the code without also entering between the space that exists between the company name.

  • Always in this format?

  • The problem with this question is the format. If the information always comes in that order the processing is simple and can be done on a line but if the order of the information varies you will need something a little more elaborate.

  • Always comes in this format and order

1 answer

0


The code below solves your problem:

$string = 'Nome Empresa 145241';
$pattern = '/(\w+) (\d+)/i';
$replacement = '${1}; $2';
$out = preg_replace($pattern, $replacement, $string);
echo $out;

recommend to take a look at the documentation of the function preg_replace

You may need to change the regex of the variable $pattern to fit the pattern of your string

  • Thank you very much, I was able to adapt to my code. Thank you so much for your help !

Browser other questions tagged

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