+ 7

How to import strings from domains.txt to an array in the index.php? Solved! Thanks to ChaoticDawg!

From this: php.net sololearn.com to this: $domains= ["php.net", "sololearn.com"]; trailing dot is not working viz. https://www.sololearn.com/Discuss/716470/why-foreach-returns-only-the-last-value-from-an-array-correctly

15th Sep 2017, 7:55 PM
JaMi
JaMi - avatar
2 ответов
+ 4
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT']; $filename = $DOCUMENT_ROOT.'path_to_file'.'file.txt'; $lines_in_file = count(file($filename)); $file = fopen($filename, 'r'); for($i = 1; $i <= $lines_in_file; $i++) { $line = fgets($file); $value = trim($line); print 'Line '.$i.': '.$value.'<br>'; } fclose($file); You can modify the code to add the $value to an array if you wish. $domains = array(); // create an empty array before the loop array_push($domains, $value); //Add this line inside the for loop to add the value to the array
15th Sep 2017, 6:56 PM
ChaoticDawg
ChaoticDawg - avatar
+ 9
Thank You very much ChaoticDawg!!! I can sleep again! It is truly working great: <?php $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT']; $domains = $DOCUMENT_ROOT.'/.../.../domains.txt'; $lines_in_file = count(file($domains)); $file = fopen($domains, 'r'); $domains = array(); for($i = 1; $i <= $lines_in_file; $i++) { $line = fgets($file); $value = trim($line); array_push($domains, $value); //print 'Line '.$i.': '.$value.'<br>'; } foreach($domains as $domain) { $ip = gethostbyname($domain); echo $domain. " " . $ip .'<br>'; } fclose($file); ?> I like sololearners You know it! and the output is [domain IP]: sololearn.com 184.168.221.12 php.net 72.52.91.14
15th Sep 2017, 7:52 PM
JaMi
JaMi - avatar