我怎么能把(破折号或下划线)放在(选择php,mysql)中的单词之间?

时间:2012-11-06 15:13:08

标签: php mysql mysql-select-db

我从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

2 个答案:

答案 0 :(得分:2)

尝试:

//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>";    
 }

答案 1 :(得分:2)

如果您要做的只是用下划线替换空格,请运行

str_replace(" ","_",$row["name"])。请参阅http://php.net/manual/en/function.str-replace.php