read javascript file with PHP - php

please help me
I would like to read a javascript file and retrieve only lines starting with url.instance = "www.google.fr"; in input
in order to be able to modify these url
$handle = fopen('javascriptfile.js', 'r');
if ($handle)
{
while (!feof($handle))
{
$buffer = fgets($handle);
//echo $buffer."<br>";
echo "<input type='text' value='$buffer'>";
}
fclose($handle);
}
I have write this code but I don't know how to continue please help me

You can search each line with stripos
<?php
$handle = fopen('javascriptfile.js', 'r');
$match = 'url.instance = "www.google.fr";';
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle);
// check each line start with : url.instance = "www.google.fr";
if (stripos($buffer, $match) === 0 ) {
echo "<input type='text' value='$buffer'>";
}
}
fclose($handle);
}

Related

How To Open all file in a folder with fopen

I want to know how to search for a string in a folder and display the line in php. I already have a snipset but it dont search in a folder but in ONE file i have tested by replacing /something/sisi.txt by /somthing/* and /something/*.txt .
The snipset:
$searchthis = "jean";
$matches = array();
$handle = #fopen("./something/sisi.txt", "r");
if ($handle)
{
while (!feof($handle))
{
$buffer = fgets($handle);
if(strpos($buffer, $searchthis) !== FALSE)
$matches[] = $buffer;
}
fclose($handle);
}
print_r($matches);
I tested scandir using PHP version 7.4 and it gave expected results.
The reason why I started the index of $i at 2 is because the first two indices refer to "." and ".." which wasn't necessary for the test.
edit: 11/7/2022 -> Additional optional Echo statements have been added to make the separation between files more clear.
The printr from the question code
More info on scandir is here:
https://www.php.net/manual/en/function.scandir.php
<?php
$dir = './something';
$files1 = scandir($dir);
$files2 = scandir($dir, 1);
//echo ("<p>Below will be the directory information...</p>");
$L = sizeof($files1);
//print_r($files1);
$results = array();
for($i = 2; $i < $L; $i++) {
//echo "<br>The value of i is: $i";
//echo "<br>The value of files1 at i is:".$files1[$i];
scanFile($files1[$i]);
}
function scanFile($filename) {
//echo("<p>Scanning the file: $filename</p>");
$searchthis = "jean";
$matches = array();
$handle = #fopen("./something/$filename", "r");
if ($handle)
{
while (!feof($handle))
{
$buffer = fgets($handle);
if(strpos($buffer, $searchthis) !== FALSE)
$matches[] = $buffer;
}
fclose($handle);
}
for ($i = 0; $i < count($matches); $i++) {
echo "$matches[$i] <br>";
}
}
?>

Trying to write content to a file I've created, it reads all 3 lines, but when I change the lines to read 2 it still prints out 3?

$my_content = "This is the first line\n
This is the second line\n
This is the third line\n";
$my_filename = "save.txt";
function file_writer(string $file_to_write, string $content_to_write){
$file = fopen($file_to_write, "w") or die("Unable to open file");
file_put_contents($file_to_write, $content_to_write);
fclose($file);
}
file_writer($my_filename, $my_content);
function file_reader(string $file_to_read, int $num_lines) {
$file = fopen($file_to_read, "r");
while(! feof($file))
{
$line = fgets($file);
echo $line;
}
}
**file_reader($my_filename, 3);**
Try this:
function file_reader(string $file_to_read, int $num_lines) {
$file = fopen($file_to_read, "r");
$c = 0;
while(! feof($file) && $c != $num_lines)
{
$c = $c+1;
$line = fgets($file);
echo $line;
}
}
Your other problem is that you have newlines after newlines.
$my_content = "This is the first line\nThis is the second line\nThis is the third line\n";
function file_reader(string $file_to_read, int $num_lines) {
$file = fopen($file_to_read, "r");
$currLineNo = 1;
while(!feof($file) && (currLineNo < $num_lines))
{
$line = fgets($file);
echo $line;
$currLineNo += 1;
}
}
Havent tried the code myself. This is roughly way you can stop the loop at arbitrary line number.

i want to store readed file output in variable

in PHP I want each string read by "fgets()" function in a separate variable.
Below is my code
$handle = #fopen("txtfile.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
echo $buffer;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
An output of this code is:
I am Demo
I want this output as:
$word1 = I
$word2 = am
$word3 = Demo
<?php
$handle = #fopen("txtfile.txt", "r");
$text = '';
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$text .= $buffer;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
$array = explode(' ', $text);
$i = 1;
foreach ($array as $word) {
${"word$i"} = $word;
$i++;
}
Now you have each word saved in a different var

Find string in file and display lines number

I'm new at PHP so I'm need help to build this script.
I have a file.txt file with following lines:
aaaa 1234
bbba 1234
aaaa 1236
cccc 1234
aaaa 1238
dddd 1234
I want to find the line with string "aaaa" and print:
String "aaaa" found 3 times at lines: 1, 3, 5.
And better it can print these lines.
I tried this code:
<?
function find_line_number_by_string($filename, $search, $case_sensitive=false ) {
$line_number = '';
if ($file_handler = fopen($filename, "r")) {
$i = 0;
while ($line = fgets($file_handler)) {
$i++;
//case sensitive is false by default
if($case_sensitive == false) {
$search = strtolower($search); //convert file and search string
$line = strtolower($line); //to lowercase
}
//find the string and store it in an array
if(strpos($line, $search) !== false){
$line_number .= $i.",";
}
}
fclose($file_handler);
}else{
return "File not exists, Please check the file path or filename";
}
//if no match found
if(count($line_number)){
return substr($line_number, 0, -1);
}else{
return "No match found";
}
}
$output = find_line_number_by_string('file.txt', 'aaaa');
print "String(s) found in ".$output;
?>
But I dont know how to count total of strings found (3) and print each found line.
Thank in advance.
There are lots of ways to do this that produce the same final result but differ in the specifics.
Assuming that your input is not large enough that you are concerned about loading it in memory all at once, one of the most convenient approaches is to use file to read the file's contents into an array of lines, then preg_grep to filter the array and only keep the matching lines. The resulting array's keys will be line numbers and the values will be whole lines that matched, perfectly fitting your requirements.
Example:
$lines = file('file.txt');
$matches = preg_grep('/aaaa/', $lines);
echo count($matches)." matches found.\n";
foreach ($matches as $line => $contents) {
echo "Line ".($line + 1).": ".$contents."\n";
}
$str = "aaaa";
$handle = fopen("your_file.txt", "r");
if ($handle) {
echo "String '".$str."' found at lines : ";
$count = 0;
$arr_lines = array();
while (($line = fgets($handle)) !== false) {
$count+=1;
if (strpos($line, $str) !== false) {
$arr_lines[] = $count;
}
}
echo implode(", ", $arr_lines).".";
}
UPDATE 2 :
$file = "your_file.txt";
$str = "aaaa;";
$arr = count_line_no($file, $str);
if(count($arr)>0)
{
echo "String '".$str."' found at lines : ".implode(", ", $arr).".";;
}
else
{
echo "String '".$str."' not found in file ";
}
function count_line_no($file, $str)
{
$arr_lines = array();
$handle = fopen("your_file.txt", "r");
if ($handle) {
$count = 0;
$arr_lines = array();
while (($line = fgets($handle)) !== false) {
$count+=1;
if (strpos($line, $str) !== false) {
$arr_lines[] = $count;
}
}
}
return $arr_lines;
}
**Try it for solve your problam **
if(file_exists("file.txt")) // check file is exists
{
$f = fopen("file.txt", "r");
// Read line by line until end of file
$row_count = 0;
while(!feof($f))
{
$row_count += 1;
$row_data = fgets($f);
$findme = 'aaaa';
$pos = strpos($row_data, $findme);
if ($pos !== false)
{
echo "The string '$findme' was found in the string '$row_data'";
echo "<br> and line number is".$row_data;
}
else
{
echo "The string '$findme' was not found ";
}
}
fclose($f);
}

Php Save fopen Result To A String

How can I get the "$buffer" value into a string, and use it outside the fopen and fclose functions? Thanks.
$handle = #fopen("http://www.example.com/", "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgetss($handle, 5000);
echo $buffer ;
}
fclose($handle);
}
Try file_get_contents() :
$buffer = file_get_contents("/my/file.txt");
$handle = #fopen("http://www.example.com/", "r");
$buffer = '';
if ($handle) {
while (!feof($handle)) {
$buffer .= fgetss($handle, 5000);
}
fclose($handle);
}
The easiest way is to use file_get_contents:
$buffer = file_get_contents("http://www.exemple.com");
$buffer is itself a string. instead of printing it using echo just concatenate it there and print it or use it with all together after the loop ends.
$buffer = '';
if ($handle) {
while (!feof($handle)) {
$buffer .= fgetss($handle, 5000);
}
fclose($handle);
}
//print the whole stuff:
echo $buffer;
And if you want to get all the stuffs only no other processing try using:
file_get_contents
$handle = #fopen("http://www.example.com/", "r");
$buffers = array();
if ($handle) {
while (!feof($handle)) {
$buffers[] = fgetss($handle, 5000);
}
fclose($handle);
}
print_r ( $buffers );

Categories