0

I create a simple script for search words inside a txt file.

<?php
$search_term = "lorem";
$file = file('textfile.txt');

foreach($file as $line_number => $line){
   $row = preg_match('/' . $search_term . '/i', $line);
   echo $row;
}

The TXT file have > 7000 lines, in total ~ 6 MB

In php 5.6, 7.0 or 7.1 the script runs in 60 - 100 ms., but in php >= 7.2 the time of execution up to 3.5 seconds.

I compare the php.ini files for each versión and I dont see any diference for PCRE options.

Can anyone help me?

Thanks in advance.

5
  • 1
    What is the pattern? Commented Nov 30, 2022 at 11:30
  • @WiktorStribiżew its a simple word. I modify question por reflect the variable. Thanks. Commented Nov 30, 2022 at 11:33
  • 1
    "its a simple word" - then why regular expressions to begin with, why not just check using stripos? Commented Nov 30, 2022 at 11:36
  • @CBroe thnaks for response. I think it, but in a future the functión can get regular expression. The question is that in php < 7.2 works perfectly, but in php 7.2+ works slowly. Commented Nov 30, 2022 at 11:38
  • Show us a minimum reproducible example. Commented Dec 11, 2022 at 5:21

1 Answer 1

0

This will probably work better for you, and its lightning fast.

$words=explode(' ',$words);
$words=implode('|',$words);
$search=shell_exec( "grep -E '{$words}' data.txt");
$search=explode('\n',$search);
foreach($search as $line){echo '<p>'.$line.'</p>';};

The method you outlined will also use a lot of ram. My example will use next to nothing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.