Iterate over each line in a string in PHP
preg_split
the variable containing the text, and iterate over the returned array:
foreach(preg_split("/((\r?\n)|(\r\n?))/", $subject) as $line){
// do stuff with $line
}
PHP - iterate on string characters
Use str_split to iterate ASCII strings (since PHP 5.0)
If your string contains only ASCII (i.e. "English") characters, then use str_split.
$str = 'some text';
foreach (str_split($str) as $char) {
var_dump($char);
}
Use mb_str_split to iterate Unicode strings (since PHP 7.4)
If your string might contain Unicode (i.e. "non-English") characters, then you must use mb_str_split.
$str = 'μυρτιὲς δὲν θὰ βρῶ';
foreach (mb_str_split($str) as $char) {
var_dump($char);
}
How to loop through each line in a file PHP
Your code will make some empty lines when erases key from file.
And you passed return value of fopen() as a parameter of file_get_contents(). the return value is resource on file, but file_get_contents() needs string(file path).
You can check following.
$gkey = $_GET["key"];
$file_path="./generatedkeys.txt";
$file = fopen($file_path, "a");
$generatedk = array();
// generate table from data in txt file
while(! feof($file)) {
$generatedk[] = fgets($file);
}
fclose($file);
for($i=0; $i<count($generatedk); $i++){
$key=$generatedk[$i];
if ($key == hash("sha256", $gkey)){
// Removing of key from data in txt file
array_splice($generatedk, $i, 1);
file_put_contents($file_path, implode("\n", $generatedk));
$accfile = fopen("./accs.txt", "a");
fwrite($accfile, hash("sha256", $key).",".hash("sha256", $hwid)."\n");
fclose($accfile);
break;
}
}
I hope it will be working well.
Thanks.
PHP while loop to read multiline text from string
If this is really a text file you want to read, then you'd be better of to use fgets()
or read the file to an array completely with file()
and use explode()
afterwards. Consider this code:
$arr = file("somefile.txt"); // read the file to an array
for ($i=0;$i<count($arr);$i++) { // loop over it
$tmp = explode(" ", $arr[$i]); // splits the string, returns an array
$firstword = $tmp[0];
$secondword = $tmp[1];
}
loop through text file for first number on each line
This gets the first number in a string:
function getFirstNumber($str){
$strlen = strlen( $str );
$num = "";
for( $i = 0; $i <= $strlen; $i++ ) {
$char = substr($str, $i, 1);
if(!is_numeric( $char)){
if(!empty($num))
return intval($num);
}else $num .= $char;
}
return intval($num);
}
$str = "asdfas1234241234lkj 1l2k3j4 1k2j3412341234";
echo getFirstNumber($str);
Here's a fiddle.
PHP :: Parse strings, while iterating through an array of substrings?
Using regex'es will produce more clear code. For example with preg_match
:
$line = 'April 01 2020 Key Information Read :: Interesting Character #1: Kermit the Frog';
$searchTerms = ["Kermit the Frog","Miss Piggy","The Muppet Movie (1979)"];
// prepare regex with named group from terms
$delimiter = '~';
$regex = $delimiter . '(?<phrase>(' . join('|', array_map(fn($term) => preg_quote($term, $delimiter), $searchTerms)) . '))' . $delimite;
// search by regex
preg_match($regex, $line, $matches);
$foundPhrase = $matches['phrase'] ?? null;
Related Topics
Php7.1 JSON_Encode() Float Issue
How to Build a JSON Array from MySQL Database
Upload 1Gb Files Using Chunking in PHP
Any Decent PHP Parser Written in PHP
How to Write a Stored Procedure Using PHPmyadmin and How to Use It Through PHP
Best Way to Determine If a Url Is an Image in PHP
How to Use PHP to Check If a Directory Is Empty
How to Put PHP Inside JavaScript
Include Constant in String Without Concatenating
Chaining Static Methods in PHP
Why Are Functions and Methods in PHP Case-Insensitive
How to Install PHP-Curl in Ubuntu 16.04
Storing Form Data as a Session Variable
Search for PHP Array Element Containing String
PHP JSON Encode - Malformed Utf-8 Characters, Possibly Incorrectly Encoded
PHP Remove Special Character from String
How to Solve Incompatible with SQL_Mode=Only_Full_Group_By in Laravel Eloquent