Replace string in txt or flat file with PHP

0

here again requesting your help and I hope you can help me create this instruction.

I want to create a php script that reads a flat file and substitutes a string of characters, that string of characters is identified with a regular expression that I created for it.

I am using preg_replace since it allows me to replace strings based on regular expressions. If you did it from the php file it works great for me, the issue is that it's a batch of files, so I have to read them, look for the regular expression, replace that information, save the file and you're done.

The most I have managed to do is the following, I know I need a touch but I could not see it. I must be doing something wrong. The sample flat file goes like this:

  

11/29/18 11:07 a. m. - Noemi Venaim Broker: Parral selling wide apt 160mts. 3hab more service. 3 bathrooms. 2 points Fitted kitchen. Young Edf with beautiful social areas. Panoramic view. Power plant. Price of opportunity Ref 35.000 NEGOTIALES.
  11/29/18 11:58 a. m. - Luisana Tavare Corredora: Luisana tavare sells land in la morocha variety of options with and without pool, more inf 04144301822

The regular expression to look for would be this:

$patron = '/\d{2}\/\d{2}\/\d{2}..............\-/m';

What will you take as a value:

$sustitucion = '|2018-11-29|';

That is, it is replaced from "11/29/18 11:58 AM -" by "| 2018-11-29 |"

And the complete code would be this:

$archivo = "prueba.txt";
$abrir = fopen('prueba.txt', 'r+');
$data = fread($abrir, filesize($archivo));

$patron = '/\d{2}\/\d{2}\/\d{2}..............\-/m';
$sustitucion = '|2018-11-29|';
$datosnuevos = preg_replace($patron, $sustitucion, $data);
fwrite($abrir, $datosnuevos);
fclose($abrir);

The result I get with this code is the following:

  

11/29/18 11:07 a. m. - Noemi Venaim Broker: Parral selling ample   apt 160mts. 3hab more service. 3 bathrooms. 2 points Fitted kitchen. Edf   Young with beautiful social areas. Panoramic view. Power plant.   Price of opportunity Ref 35.000 NEGOTIALES.
  11/29/18 11:58 a. m. - Luisana Tavare Corredora: Luisana tavare sells land plots in la morocha variety of options with and without a pool, more inf 04144301822 | 2018-11-29 | Noemi Venaim Broker: Parral I sell wide apt 160mts. 3hab more service. 3 bathrooms. 2 points Fitted kitchen. Young Edf with beautiful social areas. Panoramic view. Power plant. Price of opportunity Ref 35.000 NEGOTIALES. 2018-11-29 | Luisana Tavare Corredora: Luisana tavare sells land in la morocha variety of options with and without pool, more inf 04144301822

And what I want is for you to do this to me:

  

| 2018-11-29 | Noemi Venaim Broker: Parral I sell wide apt 160mts. 3hab more service. 3 bathrooms. 2 points Fitted kitchen. Young Edf with beautiful social areas. Panoramic view. Power plant. Price of opportunity Ref 35.000 NEGOTIALES.
  2018-11-29 | Luisana Tavare Corredora: Luisana tavare sells land in la morocha variety of options with and without pool, more inf 04144301822

Has anyone any idea how to do it?

    
asked by Jean Carlo Garcia Quiñones 04.12.2018 в 03:22
source

1 answer

1

The problem is that you opened your file with the 'r +' mode that means read and add, so it does not erase the original content.

Try this code

<?php
$archivo = "test.txt";
$leer = fopen($archivo, 'r+');
$data = fread($leer, filesize($archivo));
fclose($leer);

$patron = '/(\d{2})\/(\d{2})\/(\d{2}).+\-/m';
$sustitucion = '|20$3-$2-$1|';
$datosnuevos = preg_replace($patron, $sustitucion, $data);
echo $datosnuevos;
$escribir = fopen($archivo, 'w');
fwrite($escribir, $datosnuevos);
fclose($escribir);
?>

You can also use the file_get_contents () and file_put_contents () functions that do the opening-reading-closing and opening-writing-closing saving you a few lines, leaving something like this:

<?php
$archivo = "test.txt";
$data = file_get_contents($archivo);

$patron = '/(\d{2})\/(\d{2})\/(\d{2}).+\-/m';
$sustitucion = '|20$3-$2-$1|';
$datosnuevos = preg_replace($patron, $sustitucion, $data);
echo $datosnuevos;
file_put_contents($archivo, $datosnuevos);
?>

I also changed your pattern and substitution strings for one that uses the same values of the original data and not an arbitrary text. The php manual explains how substitutions work link :

  

replacement may contain references of the form \ n or $ n, being   preferred the last form. Each reference of this type will be   replaced by the text captured by the n-simo pattern between   parenthesis. n can be from 0 to 99, and \ 0 or $ 0 refers to the text   matched by the complete pattern. The opening parenthesis is   count from left to right (starting with 1) to get the   number of capture sub-patterns. The inverted bar must be folded   to be able to use it in the replacement (PHP string "\\").

    
answered by 04.12.2018 / 06:26
source