What is the best way to read hyperlinks from an external text file and store them in an array in PHP?
To read hyperlinks from an external text file and store them in an array in PHP, you can use the file() function to read the contents of the text file line by line. Then, you can loop through each line and use regular expressions to extract the hyperlinks. Finally, store the hyperlinks in an array for further processing.
<?php
// Read the contents of the external text file
$lines = file('external_links.txt');
// Initialize an empty array to store hyperlinks
$hyperlinks = [];
// Loop through each line to extract hyperlinks
foreach ($lines as $line) {
preg_match_all('/<a\s[^>]*href="([^"]*)"/i', $line, $matches);
if (!empty($matches[1])) {
$hyperlinks = array_merge($hyperlinks, $matches[1]);
}
}
// Print the array of hyperlinks
print_r($hyperlinks);
?>
Keywords
Related Questions
- What are common issues when working with cookies in PHP?
- How can PHP developers effectively handle user redirection based on different user groups stored in a database?
- Welche Best Practices sollten beim Umgang mit Browser-Identifikation und Referrer in PHP beachtet werden, um die Datenkonsistenz zu gewährleisten?