P
P
Pseudoquater2022-03-28 21:20:52
PHP
Pseudoquater, 2022-03-28 21:20:52

How to extract randomly unique lines from a text file in PHP?

We have a text file with the Nth number of lines of text (the lines do not have to be sentences, and not necessarily with punctuation marks).
Let's say you need to pull out 150 random lines from this file and output them.

On one of the forums I saw this code (which seems to be just to implement such an idea, however, I tried it - and each time the script displays one or two lines of text (line number: text), and the rest of the lines are just line numbers without text. ..). Please help me find the error. Thanks in advance.

-------
CODE:
<?php
# Count the number of lines in the file.
$f = fopen('semantic.txt', "r");
<?php
# Count the number of lines in the file.

function FileGetCount( $path ) {
if ( $f = fopen( $path , "r" ) ) {
$counter = 0;
while (fgets($f)) { $counter++; }
fclose($f);
return $counter;
}
return false;
}
function GetRandomArray( $num , $lineCount ) {
if ( $num > $lineCount ) {
$num = $lineCount;
}

$subFunction = function( &$result , &$num , $min , $max ) use(&$subFunction) {
if ( ( $num <= 0 ) || ( $min > $max ) ) {
return [] ;
}
$key = mt_rand( $min , $max );
$result[] = $key;
$num--;
if ( mt_rand(0,1) ) {
$subFunction( $result , $num , $min , $key-1 );
$subFunction( $result , $num , $key+1 , $max );
} else {
$subFunction( $result , $num , $key+1 , $max );
$subFunction( $result , $num , $min , $key-1 );
}
};
$result = [];
$subFunction( $result , $num , 0 , $lineCount - 1 );
return $result;
}
function EchoRandomLine( $path , $randomMap ) {
if ( $f = fopen( $path , "r" ) ) {
$last = 0;
foreach($randomMap as $line) {
rewind($f);
$newLine = $line = $line+1;
while($newLine--) {
$buffer = fgets($f);
}
echo "$line : $buffer
"; # line_number : line_self
}
fclose($f);
return true;
}
return false;
}

$path = "semantic.txt";
$countRandomLine = 150;

$countLine = FileGetCount( $path );
$randomMap = GetRandomArray( $countRandomLine , $countLine );
EchoRandomLine( $path , $randomMap );
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
ThunderCat, 2022-03-28
@ThunderCat

$arr = file('semantic.txt');
if(count($arr < 150)) {return false;}
shuffle($arr);
$res = array_slice($arr, 0, 150);
foreach($res as $str){
  echo $str;
}

P
Pseudoquater, 2022-03-29
@Pseudoquater

Bad luck, Mr. ThunderCat!
Why don't you check the code yourself when you give an answer?! (rhetorical question).
There are two errors in your code.
1. At the stage of checking the number of lines. In your example, it outputs False in any case (for a text file).
2. At the withdrawal stage. So text lines are not displayed!
Correct solution (without quantity check):

$arr = file('semantic.txt');
shuffle($arr);
$res = array_slice($arr, 0, 150);
foreach($res as $str){
  echo htmlspecialchars($str) . "<br />\n";
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question