Php while inside for - What's wrong with this code? - php
This code is to read contents of a text file that contains 100 urls one in each line. The script is to search for a particular word in the urls using file_get_contents.
<?php
$mysearch = file("phpelist.txt");
for($index = 0; $index <count($mysearch); $index++)
{ while ($index >=10 && $index <=20 ):
$mysearch[$index] = str_replace("\n", "", $mysearch[$index]);
$data = file_get_contents("$mysearch[$index]");
$searchTerm = 'about';
if (stripos($data, $searchTerm) !== false) {
echo "$mysearch[$index]</strong>...FOUND WORD<br><strong>";
}
else
{
echo "$mysearch[$index]</strong>...NO SUCH WORD<br><strong>";
}
endwhile;
}
?>
Your code confused me. Looking at just the requirements statement, I got ...
$searchTerm = 'about';
$file = new SplFileObject('phpelist.txt');
foreach ($file as $n => $url) {
if ($n < 10) continue;
if ($n > 20) break;
$content = file_get_contents($url);
if (stripos($content, $searchTerm) !== false) {
echo "<strong>$url</strong>...FOUND WORD<br>";
} else {
echo "<strong>$url</strong>...NO SUCH WORD<br>";
}
}
CORRECTED: Additional requirements (see comments below). Which means its no longer much short than OP. The only thing it adds, I guess, is the use of SplFileObject. I like using SplFileObject because it lets you use a file like an array (without loading the entire file into memory) and so can be used in a foreach (since SplFileObject implements the Iterator interface).
Related
what is the best way for search in json file in php?
hi i have many data files in json format in a folder. now i want to search a filed in them .my search word maybe not exist in some of them and may be exist in one of them files. i have read this function and if not exits in a file i call the function to read another file. when i echo the result show me and works fine but return not working and no data returned. function get_shenavari_in_files($search,$type) { static $counter =1 ; $darsadi = 0; $find = false; $file_name = get_files_in_dir(); // make an array of file names $file_number = count($file_name)-$counter ; $file="files/" .$file_name[$file_number]; $file_data = read_json($file); for($i = 0 ; $i<count($file_data) ; $i++) { if($file_data[$i][$type] == $search ) { $darsadi = $file_data[$i]['darsadi'] ; $find = true; echo $darsadi ; //this works and show the data return $darsadi; // this is my problem no data return. break; } } if($find == false) { $counter ++; get_shenavari_in_files($search,$type); } } var_dump(get_shenavari_in_files('Euro','symbol')); //return null
Once you recurse into get_shenavari_in_files, any found value is never returned back to the inital caller, i.e. instead of if($find == false) { ... get_shenavari_in_files($search,$type); } you simply need to prepend the function call with a returnstatement if($find == false) { ... return get_shenavari_in_files($search,$type); } Having said that, I would try a much simpler (and thereby less error-prone) approach, e.g.: function get_shenavari_in_files($search, $type) { $files = glob("files/*.json"); // Get names of all JSON files in a given path $matches = []; foreach ($files as $file) { $data = json_decode(file_get_contents($file), true); foreach ($data as $row) { if (array_key_exists($type, $row) && $row[$type] == $search) { $matches[$file] = $search; } } } return $matches; } This way, you would be able to eliminate the need for a recursive call to get_shenavari_in_files. Also, the function itself would become more performant because it doesn't have to scan the file system over and over again.
Prevent glob from selecting a file with a specific file name
I have a folder with 5 html files. The files are called: page-1.html page-2.html page-hello.html page-32.html yo-page-text.html I wish to use glob function to return an array with only the files formatted: "page[number 0 - 1000]". So essentially I wish the glob function to return the following pages from the example above: page-1.html page-2.html page-32.html This is the code that I have managed to write so far: <?php $directory = "testfolder/"; foreach (glob($directory . "page-*.html") as $filename) { echo basename($filename); } ?>
The glob pattern is not as flexible as RegEx, so unless someone has some other magic, you can filter after the glob: $files = preg_grep('/page-[0-9]+\.html/', glob($directory . '*.*')); If the glob pattern supported repetition + then it would be easy. Bash and Korn shells offer it with +([0-9]) but PHP doesn't. You could also check for one number and trust that what follows matched by * will be numbers with: glob($directory . 'page-[0-9]*.html'), but this could match page-0-hello.html as well.
Assuming that your code works and that you get the basename of the files: $name = basename($filename); //do not echo the basename for ($i = 0; $i <= 1000; $i++) { $trname = "page-$i"; if ($name == $trname) { echo "$name"; } } Put one of these codes inside the foreach and get rid of echo basename($filename);. I'd recommend using the second one as nesting loops may prove to be a problem. Note: This code can only work if your one works. IMPORTANT EDIT: My guess id that you want to do something (not echoing) the names, in which case: function foo(bar) { $arr = array(); $name = basename($filename); $name = str_replace("page-", "", $name); if (intval($name) < 1000 && intval($name) >= 0) { $arr[] = $name; } } Then, you can call on the function this way: function foo() { $arr = array(); $directory = "testfolder/"; foreach (glob($directory . "page-*.html") as $filename) { $name = basename($filename); $name = str_replace("page-", "", $name); if (intval($name) < 1000 && intval($name) >= 0) { $arr[] = $name; } } } $bar = foo(); foreach ($bar as $obj) { //do something }
looping through txt file to use specific part of a string
I am new to Php and can't seem to figure this out no matter how much I've googled. So I've opened the txt file (which consists of multiple lines of this type of string unique Identifier IMEI in bold: Rx:00:39:54 06/09/2015:+RESP:GTEPS,210101,863286020022449,,8296,01,1,3,0.0,0,1031.1,29.367950,-30.799161,20150906003710,,,,,,2857.9,20150906003710,8038$) There are different strings with different IMEIs but i only want to use a specific one. My question is, how do I extract/only use the string with the same Unique identifier and then loop through those to use in another function? My function has different cases and each case has different calculations, so I'll need to loop through the txt file (with e.g. 863286020022449 as Identifier, ignoring other identifiers/IMEIs) in order to use the string in my function as below: This is my starter function: function GetParam($unknownFunction, $numberCommas) { $returnString = ""; $foundSting = 0; $numberFound = 0; $len = strlen($unknownFunction); for ($i = 0; $i < $len; ++$i) { if ($Rawline[$i] == ",") { ++$numberFound; if ($numberFound > $numberCommas) break; if ($numberFound == $numberCommas) $foundSting = 1; } else if ($foundSting == 1) { $returnString .= $unknownFunction[$i]; } } return $returnString; echo $returnString; } $i = strpos($unknownFunction, ":GT"); $p = substr($unknownFunction, $i+3,3); $Protocol = GetParam($unknownFunction, 1); //this switch reads the differences in the message types (e.g. HBD- in this case is a heartbeat message type and would thus have a different amount of commas in the string and has different definitions of the characters within the commas) switch ($p) { case 'HBD': //+ACK:GTHBD,220100,135790246811220,,20100214093254,11F0$ //This is an example of an HBD message $result2["Type"] = 'Heart beat'; $IMEI = GetParam($unknownFunction, 2); $mDate = GetParam($unknownFunction, 4); $mDate = substr($mDate,0,4).'-'.substr($mDate,4,2).'- '.substr($mDate,6,2).' '.substr($mDate,8,2).':'.substr($mDate,10,2).':'.substr($mDate,12,2); break; This is the biggest problem I am facing at the moment and when I print the different lines, it indicates the correct IMEI but it does not loop through the whole file to use each string that belongs to that IMEI. Your assistance would be greatly appreciated. Thank you so much. Example of input file: Rx:00:00:00 28/02/2018:+RESP:GTFRI,3C0103,862045030241360,,14067,11,1,1,29.7,320,151.1,30.949307,-29.819685,20180227235959,0655,0001,013A,87B6,00,35484.1,01500:51:31,,,100,220101,,,,20180228000000,3461$ Rx:00:00:01 28/02/2018:+RESP:GTERI,380201,869606020047340,gv65,00000002,14076,10,1,1,119.0,119,24.3,18.668516,-34.016808,20180227235955,0655,0001,00F7,2DC9,00,98912.0,02235:20:25,0,100,220101,0,0,20180227235958,FF20$ Rx:00:00:03 28/02/2018:+RESP:GTERI,380201,869606020162990,,00000002,12912,10,1,1,0.0,230,1127.3,30.846671,-27.674206,20180227235956,0655,0001,013E,88B0,00,106651.1,03546:44:42,0,100,210101,0,0,20180227235959,6190$ Rx:00:00:03 28/02/2018:+ACK:GTHBD,450102,865084030005340,gb100,20180228000003,CC61$ Rx:00:00:03 28/02/2018:+RESP:GTERI,380201,869606020115980,,00000002,13640,10,1,1,12.1,353,1663.1,28.580726,-28.162208,20180227235957,,,,,,37599.6,02422:07:24,0,100,220101,0,0,20180228000000,1937$ Rx:00:00:04 28/02/2018:+RESP:GTERI,380502,869606020276840,gv65,00000002,12723,10,1,1,0.0,106,1232.8,22.878013,-27.951762,20180227235952,0655,0001,0204,63C5,00,13808.9,00778:32:20,0,100,210100,0,0,20180228000002,2C50$ Rx:00:00:04 28/02/2018:+RESP:GTERI,380502,869606020274530,gv65,00000002,12683,10,1,1,0.0,91,1213.7,24.863444,-28.174319,20180227235956,0655,0001,0203,69F1,00,9753.2,00673:49:21,0,100,210100,0,0,20180228000003,8AC7$ Rx:00:00:05 28/02/2018:+ACK:GTHBD,380201,863286023083810,,20180228000003,0D87$ Rx:00:00:06 28/02/2018:+RESP:GTFRI,3C0103,862045030241360,,14086,10,1,1,34.0,327,152.0,30.949152,-29.819501,20180228000002,0655,0001,013A,87B6,00,35484.1,01500:51:36,,,100,220101,,,,20180228000005,3462$ Rx:00:00:06 28/02/2018:+ACK:GTHBD,060228,862894021626380,,20180228000007,F9A5$ Rx:00:00:07 28/02/2018:+RESP:GTERI,380201,869606020019430,,00000002,12653,10,1,1,0.0,219,1338.7,26.882063,-28.138099,20180228000002,,,,,,86473.7,05645:48:34,0,93,210101,0,0,20180228000003,0FA5$ Rx:00:00:09 28/02/2018:+ACK:GTHBD,380502,869606020233940,gv65,20180228000008,7416$ Rx:00:00:10 28/02/2018:+RESP:GTAIS,380201,869606020171710,,11,11,1,1,0.0,95,281.2,30.855164,-29.896575,20180228000009,0655,0001,0156,9A9F,00,156073.7,20180228000008,F9A4$ Each GT message means something which is why i need to extract only one specific IMEI and use the result in my function as a breakdown of what every set of numbers between the commas actually mean. The end result needs to be populated in an excel spreadsheet but that's a different issue.
Nested foreach, keeping tracking of the IMEIs you've already gone through. Or something like this. <?php $filename = 'info.txt'; $contents = file($filename); foreach ($contents as $line) { $doneAlreadyArray = array(); $IMEI = GetParam($line, 2); foreach ($contents as $IMEIline){ $thisIMEI = GetParam($IMEIline,2); //check if already done the IMEI previously if (!in_array($thisIMEI, $doneAlreadyArray)){ //matching IMEIs? if ($thisIMEI == $IMEI){ //run new function with entire $IMEIline new_function($IMEIline); } } } //add IMEI to doneAlreadyArray array_push($doneAlreadyArray,$IMEI); } ?>
If I've understood your question right and you want to extract the string(line) with the same Unique identifier, this may be useful for your needs as a strating point. The example is very basic, and use data from your question: <?php // Read the file. $filename = 'input.txt'; $file = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); // Each item of $output will contain an array of lines: $output = array(); foreach ($file as $row) { $a = explode(',', $row); $imei = $a[2]; if (!array_key_exists($imei, $output)) { $output[$imei] = array(); } $output[$imei][] = $row; } // Then do what you want ... foreach ($output as $key=>$value) { echo 'IMEI: '.$key.'</br>'; foreach($value as $row) { // Here you can call your functions. I just echo the row: echo $row.'</br>'; } } ?>
thank you for the feedback. Ryan Dewberry ended up helping me. The fix was simpler than I thought too :) //Unknownfunction is now $line function GetParam($line, $numberCommas) { $returnString = ""; $foundSting = 0; $numberFound = 0; $len = strlen($line); for ($i = 0; $i < $len; ++$i) { if ($line[$i] == ",") { ++$numberFound; if ($numberFound > $numberCommas) break; if ($numberFound == $numberCommas) $foundSting = 1; } else if ($foundSting == 1) { $returnString .= $line[$i]; } } return $returnString; // print $returnString; } //this is new - makes sure I use the correct IMEI $contents = file($fileName); foreach ($contents as $line){ $haveData = 0; $IMEI = GetParam($line, 2); if ($IMEI == $gprsid){ $i = strpos($line, ":GT"); $p = substr($line, $i+3,3); $Protocol = GetParam($line, 1); //this is the part I struggled with as well - This is an array of all of my //calculation //results and in printing it out I can see that everything is working $superResult = array(); array_push($superResult,$result2); print_r($superResult); } } Much appreciated. Thank you!
Sorting files in an array by the ocurrences of a word in it, php
I'm making a search bar that searches files in a directory that have the word searched, then I want it to be added to an array by order of which one has more times the word asked to the one with less. I'm working on PHP this is my code: <?php if(isset($_POST['busqueda'])){ $variable = utf8_encode($_POST['busqueda']); } $Array1 = array(); foreach(glob("*.txt") as $filename) { $contents = file_get_contents($filename); if (strpos($contents, $variable)){ $Array1[] = $filename; } } I don't know how to do it exactly, I think that I should use substr_count(file_get_contents($Array1[$position1])) or something like that but I'm unsure how to make the sorting system, can someone help me! print_r($Array1); for($var1=0; $var1<sizeof($Array1); $var1++){ echo "times on the file: ".$Array1[$var1]."<br>"; echo substr_count(file_get_contents($Array1[$var1])); } ?>
You can use the substr_count itself. Then you need to use arsort to sort the array. $Array1 = array(); foreach (glob("*.txt") as $filename) { $contents = file_get_contents($filename); if ( ($count = substr_count($contents, $variable)) ) { $Array1[$filename] = $count; } } arsort($Array1) ; print_r($Array1); foreach ($Array1 as $file => $count) { echo "times on the file($file): $count <br>"; }
Bash (available on at least Linux and Mac operating systems) makes it extremely easy to accomplish your task, because you can call commands through PHP's exec function, assuming it is not disabled by an administrator. If you're on Windows, then this will probably not work, but most people are using Linux for a production environment, so I thought this answer would be worthy of posting. The following function is taken from CodeIgniter's file helper and only serves to fetch an array of filenames from a specified directory. If you don't need a function like this because you are getting your filenames from somewhere else, just note that this function can include the full file path for each file, and that's why I used it. function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE) { static $_filedata = array(); if ($fp = #opendir($source_dir)) { // reset the array and make sure $source_dir has a trailing slash on the initial call if ($_recursion === FALSE) { $_filedata = array(); $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; } while (FALSE !== ($file = readdir($fp))) { if (#is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0) { get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE); } elseif (strncmp($file, '.', 1) !== 0) { $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file; } } return $_filedata; } else { return FALSE; } } Now that I can fetch an array of filenames easily, I'd do this: /** * Here you can see that I am searching * all of the files in the script-library * directory for the word "the" */ $searchWord = 'the'; $directory = '/var/www/htdocs/script-library'; $filenames = get_filenames( $directory, TRUE ); foreach( $filenames as $file ) { $counts[$file] = exec("tr ' ' '\n' < " . $file . " | grep " . $searchWord . " | wc -l"); } arsort( $counts ); echo '<pre>'; print_r( $counts ); echo '</pre>'; For a good explaination of how that works, see this: https://unix.stackexchange.com/questions/2244/how-do-i-count-the-number-of-occurrences-of-a-word-in-a-text-file-with-the-comma I tested this code locally and it works great.
looking for a word in multiple files in directories
I'm working on a searchmachine for my blog (http://iljalicious.net/suche/) and I'm sorry that the used language is not English on the website but I translated the code for you: $lookingfor = $_POST['word']; $lines = file('../archiv/2015/juli/eintraege.dat'); $found = false; foreach($lines as $line) { if(strpos($line, $lookingfor) !== false) { $found = true; echo $line; } } if(!$found) { echo 'I found nothing, dood!'; } Currently, the function is working perfectly, but only if it is looking for a word in one file ('http://iljalicious.net/archiv/2015/juli/eintraege.dat'). On my blog, I have such a file for every month and I want my searchmachine to look for a word in every file called 'eintraege.dat' in the directory '../archiv/' and subdirectories as '2015','2016' etc. How can I solve this problem? Greetings, iljalicious ****EDIT***** Found another..kind of stupid solution: The code is not translated, but is the same as above, the only thing I changed was: From: $lines = file('../archiv/2015/juli/eintraege.dat'); To: $archiv = file('../suche/archiv.php'); And it worked. $suchbegriff = $_POST[suchbegriff]; $archiv = file('../suche/archiv.php'); $gefunden = false; foreach($archiv as $zeile) { if(strpos($zeile, $suchbegriff) !== false) { $gefunden = true; echo $zeile; } } if(!$gefunden) { echo '<div class="information">'. 'Alter, wonach hast du gesucht?<br>'. 'Mhm... Soso, <a>'. $_POST[suchbegriff]. '</a> also! '. 'Dein Ernst?<br>'. 'Als ob ich über sowas berichte O;'. '</div>'; }
You can try using glob, The glob() function searches for all the pathnames matching pattern according to the rules used by the libc glob() function, which is similar to the rules used by common shells. Something similar to this: $file_list = []; foreach (glob("archive*/eintraege.dat") as $filename) { $file_list[] = $filename; } $lookingfor = $_POST['word']; foreach ($file_list as $file) { $lines = $file; $found = false; foreach ($lines as $line) { if (strpos($line, $lookingfor) !== false) { $found = true; echo $line; } } if (!$found) { echo 'I found nothing, dood!'; } } Note: glob work only with files hosted on your server. So if you want to search from the root of your server, the function argument should be something like : /var/www/site/content/archieve/*/eintraege.dat if you want to search in the same folder of your file: */eintraege.dat