1

我想将文本和逗号分隔的文本转换为链接和类,例如:

facebook.com,facebook
twitter.com,twitter
youtube.com,youtube

逗号之前的文本将是链接,逗号之后的文本将是链接类属性,例如

$link = text before comma
$class= text after comma
<a href="$link" class="$class">$class</a>

并使用循环获取所有列表。请不要放一半代码,把我当作初学者并帮助我,谢谢

4

2 回答 2

3

好像您正在尝试从数据库中提取。我会在这里冒昧地说你只是在使用基本mysql_*功能。

$sql = "SELECT columnName from `tableName`";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($res);

foreach(explode(PHP_EOL, $row['columnName']) AS $line){
    // PHP_EOL is a line ending for that system, if you are positive that the column
    // is separated by \n, or \r\n then replace that instead of PHP_EOL
    list($link, $lcass) = explode(',', $line);

    echo '<a href="'.$link.'" class="'.$class.'">'.$class.'</a><br />';
}

我认为 $lcas 将是 $class

于 2012-07-29T03:24:24.400 回答
0

像这样做:

$file = fopen('csv_file.csv', 'r');
$data = array();
while (($line = fgetcsv($file)) !== FALSE) {
   $data[] = array('link' => $line[0], 'class' => $line[1]);
}
fclose($file);

foreach ($data as $line) {
   $link = $line['link'];
   $class= $line['class'];
   echo "<a href='$link' class='$class'>$class</a>
}
于 2012-07-29T03:07:30.187 回答