我从mysql
中选择了一些行,我想在单词之间进行下划线
这样:
<?php
$query='SELECT * FROM subcates WHERE maincates_id=1';
$result=mysql_query($query);
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
echo "<li><a href='".$row['name'].".php?c=".$row['id']."&gov=1&h=1'>".$row['name']."</a></li>";
}
?>
选择名称给我这个:
在database ==> first second
我想要selected ==> first_second
thx
尝试:
//Considering its the $row['name'] that you want in first_second format
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
$row['name'] = str_replace(' ', '_', $row['name']);
echo "<li><a href='".$row['name'].".php?c=".$row['id']."&gov=1&h=1'>".$row['name']."</a></li>";
}
编辑:
$query='SELECT * FROM subcates WHERE maincates_id=1';
$result = mysql_query($query);
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
if( current($result) === 'First second' ) {
$new_name = str_replace(' ', '_', $row['name']);
echo "<li><a href='".$new_name.".php?c=".$row['id']."&gov=1&h=1'>".$new_name." </a></li>";
}
else
echo "<li><a href='".$row['name'].".php?=".$row['id']."&gov=1&h=1'>".$row['name']." </a></li>";
}
如果您要做的就是用下划线替换空间,请运行
str_replace(" ","_",$row["name"])
。请参阅http://php.net/manual/en/function.str-replace.php。