Pregunta

I have a CSV file with this content that is downloaded from my web app:

Date,Name
2013-10-09T12:11:00+01:00,SomeName
2013-10-09T12:15:00+01:00,OtherName

Now I am writing a Behat scenario to check if the content of the downloaded file is correct, so in my step definition I have this HEREDOC string representing the CSV file that should be downloaded:

$expectedData = <<<CSV
Date,Name
2013-10-09T12:11:00+01:00,SomeName
2013-10-09T12:15:00+01:00,OtherName

CSV;

What I want to do is to replace the dates you can see in the $expectedData string by a regular expression, since in my Behat scenario I can't know what dates will appear in the downloaded file... is it possible?

P.S. Sorry for the quality of this question but I'm very new to all of these languages/tools/frameworks...

¿Fue útil?

Solución

Yes sure it can be done using /m (MULTILINE) switch. Try this code:

$expectedData = <<<CSV
Date,Name
2013-10-09T12:11:00+01:00,SomeName
2013-10-09T12:15:00+01:00,OtherName
CSV;

$re = '/^.+$/'; // some regex here to be placed in 1st column

echo preg_replace('/^(=\d{4}-\d{2}-\d{2})[^,]+/m', $re, $expectedData);

OUTPUT:

Date,Name
/^.+$/,SomeName
/^.+$/,OtherName

Otros consejos

Value of $expectedData is normal string so you can use it with functions like str_replace() or preg_replace().

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top