将字符串转换为星号

时间:2013-03-18 01:16:53

标签: php

我在基于php的系统上工作。它的一个功能是允许用户下载包含我数据库中某个表中所有信息的excel文件。我的问题是,该信息的一个数据被分类到其他用户。因此,我想将该数据的输出转换为星号字符串。

<?PHP


 //MySQL Database Connect
 include 'datalogin.php'; 

 function cleanData(&$str)
 { 
 $str = preg_replace("/\t/", "\\t", $str); 
 $str = preg_replace("/\r?\n/", "\\n", $str); 
 if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"'; 
 }
 # filename for download 
$filename = "website_data_" . date('Ymd') . ".xls"; 
 header("Content-Disposition: attachment; filename=\"$filename\""); 
 header("Content-Type: application/vnd.ms-excel");  
 $flag = false;

 //$result = pg_query("SELECT * FROM data_mapping ORDER BY CE_Hostname") or die('Query     failed!'); 
/*$query = "SELECT * FROM data_mapping";

while(false !== ($row = pg_fetch_assoc($result))) {  



//while(false !== ($row = pg_fetch_assoc($result))) {  


//$result=mysql_query($query);

$row=mysql_fetch_array($result);
$row=mysql_fetch_assoc ($result);


//$row=$row['Cust_Segment'];
// foreach($data as $row) 

 # display field/column names as first row
 if(!$flag) { 
 echo implode("\t", array_keys($row)) . "\r\n"; 

 $flag = true;  
 }
 array_walk($row, 'cleanData'); 
 echo implode("\t",($row)) . "\r\n";  

 }
 */


 $sql = 'SELECT CE_Hostname, Cust_Segment, Cust_Site_Name, CE_WAN_IP_Addr,     CE_Bkp_IP_Addr, Cust_Name, Svc_Type, com_string FROM data_mapping';

 $result = mysql_query($sql);

 if (!$result) {
    echo "DB Error, could not query the database<br>";
    echo 'MySQL Error: ' . mysql_error();
    exit;
    }
echo "Hostname\t Group/System\t Site Name\t IP ADDR\t BKP IP ADDR\t System Name\t     Device Type\t Comm_String\r\n";
     while ($row = mysql_fetch_assoc($result)) {
    echo implode("\t",($row)) . "\r\n";  
    }

 mysql_free_result($result);


//exit; 

 ?>

我想只转换comm_string结果。感谢。

2 个答案:

答案 0 :(得分:0)

$sql = 'SELECT CE_Hostname, Cust_Segment, Cust_Site_Name, CE_WAN_IP_Addr,     CE_Bkp_IP_Addr, Cust_Name, Svc_Type, com_string FROM data_mapping';

可以简单地更新为不包括分类字段

$sql = 'SELECT CE_Hostname, Cust_Segment, Cust_Site_Name, CE_WAN_IP_Addr,     CE_Bkp_IP_Addr, Cust_Name, Svc_Type FROM data_mapping';

或者,如果您仍希望在PHP代码中没有任何更改的情况下显示某些值,那么您可以

$sql = 'SELECT CE_Hostname, Cust_Segment, Cust_Site_Name, CE_WAN_IP_Addr,     CE_Bkp_IP_Addr, Cust_Name, Svc_Type, 'SECRET' FROM data_mapping';

然后会显示SECRET而不是代码

答案 1 :(得分:0)

试试这个:

while ($row = mysql_fetch_assoc($result))
{
    $row['Comm_String'] = preg_replace( '/./', '*', $row['Comm_String'] );
    echo implode("\t",($row)) . "\r\n";  
}