please help a brother!
I'm writing a script to be able to read user's .txt file in order to search a specific keyword, say "model number" and display in the textarea of my html page. I wanted to achieve this extraction on real-time base, i.e the the PHP Function preg_greg
should accept attach file variable(dynamic) NOT static as appeared here preg_greg('config.txt')
without saving the file into the dbase. following are both my php and javascript codes, respectively.
PHP:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<form enctype="multipart/form-data" method="post" action="?">
<div>
<input type="file" name="file">
<textarea cols="30" rows="20">
<?php
$lines = file('config.txt');
$lines = preg_grep("/Jibrin/i", $lines);
foreach($lines as $name){
echo "$name";
}
?>
</textarea>
</div>
</form>
</body>
</html>
Javascript:
let input = document.querySelector('input');
let textarea = document.querySelector('textarea');
input.addEventListener('change', () => {
let files = input.files;
if(files.length == 0) return;
const file = files[0];
let reader = new FileReader();
reader.onload = (e) => {
const file = e.target.result;
const lines = file.split(/\r\n|\n/);
textarea.value = lines.join('\n');
};
reader.onerror = (e) => alert(e.target.error.name);
reader.readAsText(file);
});