In a text file, ugly lines, for example:

0000000438, add, file 10/24 / 2015.06: 57: 06, "Bank", "6.20.31.32.00", "2031 / Main",

I brought them to the following form:

10.24.2015 06:57:06 Bank 6.20.31.32.00 2031.Main

I did this as follows:

$a_temp = file("temp66.txt"); $num = count($a_temp); //количество строк в файле temp66.txt for ($i = 0; $i < $num; $i++) { $read = $a_temp[$i]; $string = substr($read, 20, -2); //припудрим строки $trans = array("/" => ".", "," => " ", "\"" => ""); $print = strtr($string, $trans); 

But the other day I discovered that the lines are not formatted.
How to format strings in a different way?

Closed due to the fact that off-topic participants are Vladimir Martyanov , Pavel Parshin , user194374, aleksandr barakin , Grundy 21 Feb '16 at 21:00 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Community spirit, aleksandr barakin, Grundy
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • What does "not formatted" mean? - Vladimir Martyanov
  • Vladimir, apparently this piece of code does not work. - Jean-Claude
  • The given code and example work - eval.in/521630 - splash58
  • Have worked. Fell off this week. It is more interesting to me, why would it all of a sudden ... Maybe I have an ugly version of the implementation due to the fact that I am a beginner, but he doesn’t have to work !!!! I'll leave Jean-Claude for now through the explode, until I figure out the secret of the Madrid court @ splash58 - Yulia
  • and what this option works, and your not? on the same input data? - splash58

2 answers 2

As an option using explode ():

 $string = '0000000438,add,file,10/24/2015,06:57:06,"Bank","6.20.31.32.00","2031/Main",'; $trans = array("/" => ".", "\"" => ""); $string = strtr($string, $trans); $arr = explode(',', $string); unset($arr[0]); unset($arr[1]); unset($arr[2]); unset($arr[8]); $arr = implode(' ', $arr); echo $arr; 

Completely your code accordingly will be as follows:

 $a_temp = file("temp66.txt"); $num = count($a_temp); //количество строк в файле temp66.txt for ($i = 0; $i < $num; $i++) { $string = $a_temp[$i]; $string = substr($string, 20, -2); //припудрим строки $trans = array("/" => ".", "\"" => ""); $string = strtr($string, $trans); $arr = explode(',', $string); unset($arr[0]); unset($arr[1]); unset($arr[2]); unset($arr[8]); $arr = implode(' ', $arr); echo $arr; 
  • Replace the slash on the point forgot. - VenZell
  • @VenZell corrected. - Jean-Claude
  • @ Jean-Claude thanks! But do you have any idea why my version, which worked all the time without fail, suddenly fell off? I was upset. ( - Yulia

It is possible to try through patterns

 $string = '0000000438,add,file,10/24/2015,06:57:06,"Bank","6.20.31.32.00","2031/Main",'; $new = implode(" ", preg_replace("[/]", ".", array_slice(preg_split("/\"?[\s,]\"?+/", $string), 3, 5))); var_dump($new); 

We split the string into an array via preg_split , then array_slice throws out unnecessary elements of the array, preg_replace replaces " / " with " . ", And finally implode collects the string back