Regex is not working with file_get_contents

Asked

Viewed 67 times

2

I have a problem about regex and I come here to ask you for help.

I’m trying to get a file value .chr by regex, but it does not return any match, I find it strange because I have tested in regex engines online and gave match.

 $conteudo = file_get_contents($chart);

 preg_match('/valorLoteIni=./', $conteudo, $matches);

 var_dump($matches); exit;

In case I’m trying to get the value of valueLoteIni

inserir a descrição da imagem aqui

As I said before, I have already tested on websites, and the result was the following:

inserir a descrição da imagem aqui

As you can see, the regex is working.

I hope you help me, thank you and even more.

EDIT.

For those who want to test I will post the file: https://drive.google.com/file/d/1SuOd6ZvBz1xxNculHgy-eN2-coOBQW3a/view?usp=sharing

  • If you put a echo $conteudo; shows something?

  • shows the file contents

  • How strange.. could test with the file?

  • I used the same file. Chr to get the data.

  • Okay, thank you so much for your return man. It helped me a lot

  • This file . Chr is plain text, like a . txt?

  • I posted the file in the post

  • arranged the link!

Show 3 more comments

3 answers

1


The archive .chr has a type of encoding that adds "spaces" between characters when read by file_get_contents(). See how it is returned in the browser:

inserir a descrição da imagem aqui

But these spaces are not common spaces, they are spaces of some kind of code that is interpreted by the browser as a space. Therefore, it would be useless to put spaces in the regex, because it would not recognize them.

In this case, you can capture the pattern by adding a point . between each letter of the string sought, because the point in the regular expression represents any character. Therefore, no matter which character is that of the spaces, the pattern will marry.

That is, exchange the pattern for this:

'/v.a.l.o.r.L.o.t.e.I.n.i.=.{2}/'

Staying:

preg_match('/v.a.l.o.r.L.o.t.e.I.n.i.=.{2}/', $conteudo, $matches);

I added the {2} so that the last point matches at least 2 characters after the =.

  • Thanks man!!! worked here. THANK YOU very much indeed!!

1

I found a better solution to the problem!

Just convert the file to UTF-8 using the function mb_convert_encoding()!

Follow the code below:

    $conteudo = mb_convert_encoding(file_get_contents($chart), 'UTF-8', 'UTF-16LE');

    preg_match("/valorLoteIni=[0-9]{1-4}/", $conteudo, $match);

    $replacement =  'valorLoteIni='.$valor;

    $regex = preg_replace("/valorLoteIni=\d/", $replacement, $conteudo);

0

Good by default PCRE treats the string as a single line, but even so I believe it should marry the result of its regexp.

Try using the multiline modifier and see if you can get some results.

preg_match('/valorLoteIni=./m', $conteudo, $matches);

if the value of valueLoteIni is always an integer Voce can change to

preg_match('/valorLoteIni=\d/m', $conteudo, $matches);
  • Hello, thanks for the reply. I added m na regex but no results.

  • It is possible to share the dump of the variable $conteudo?

  • I think I better share the file, no?

  • The flag m only affects the behavior of markers ^ and $: without the m, they correspond to the beginning and end of the string, with the m, they also correspond to the beginning and end of a line. But since regex does not have ^, $ or \n, whether or not to use the m makes no difference (see). The problem must be the contents of the file, which must have invisible characters in the middle of the string, or something like that...

Browser other questions tagged

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