Hello I am trying to create a small php script that I have a question about all this script is suppose to do is read a local .txt file stored on my server with a list of plain urls. the script will grab 100 random urls from the .txt file and echo them into clickable anchor text anchor links based on anther text file with my keywords loaded 1 on each line. I cant figure out how to make this happen please review code below as its as far as I got. Example
list.txt
url1
url2
url3
---- need Output
keyword.txt
anchor text word 1
anchor text word 2
anchor text word 3 ect..
100 or whatever number i set.
Goal:
echo '<a href="url">anchor text word</a>'; << up to 100 or limit I set
$lines = file("list.txt");
$keywords = file("keywords.txt");
Shuffle($lines);
Shuffle($keywords);
function url_to_clickable_link($lines) {
return preg_replace(
'%(https?|ftp)://([-A-Z0-9-./_*?&;=#] )%i',
'<a target="blank" rel="nofollow" href="$0" target="_blank">$0</a>', $lines);
}
Echo implode("<br>", array_slice(url_to_clickable_link($lines), 0, 100));
Got stuck did not work.
CodePudding user response:
- Function names are starting lower case.
- No regex needed.
$lines = file("list.txt");
$keywords = file("keywords.txt");
shuffle($lines);
shuffle($keywords);
for ($i = 0; $i < 100; $i) {
echo "<a href='$lines[$i]' target='_blank' rel='nofollow'>$keywords[$i]</a><br>\n";
}