我有一个txt文件。
source.txt:
test.com,Test
www.cnn.com,CNN
twitter.com,Twitter
我想像打印
输出:
<a target="_blank" href="http://test.com">Test</a>
<a target="_blank" href="http://www.cnn.com">CNN</a>
<a target="_blank" href="http://twitter.com">Twitter</a>
我的代码不起作用:
$array = explode("\n", file_get_contents('/home/source.txt'));
echo '<a target="_blank" href="http://' . $array[0] . '">'. $array[1] . '</a>'
// explode file by end off line
$array = explode("\n", file_get_contents('/home/source.txt'));
// here $array[0] should be "test.com,Test"
// lets loop
foreach($array as $item) {
// now create array from our line of text
$data = explode(",", $item);
// output
echo '<a target="_blank" href="http://' . $data[0] . '">'. $data[1] . '</a>';
}
更好的?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句