Php 使用preg替换为数组

Php 使用preg替换为数组,php,Php,我有一个复选框列表,它创建了一个数组,我希望迭代该数组,使值同时创建图像文件名以及title和alt标记 $specs = get('speciesCommon'); $thepath = get_bloginfo('template_directory'); foreach($specs as $speci){ echo '<li><img src="'.$thepath.'/library/images/icons/species/'.$speci.'.gif" widt

我有一个复选框列表,它创建了一个数组,我希望迭代该数组,使值同时创建图像文件名以及title和alt标记

$specs = get('speciesCommon');
$thepath = get_bloginfo('template_directory'); 
foreach($specs as $speci){
echo '<li><img src="'.$thepath.'/library/images/icons/species/'.$speci.'.gif" width="30" height="30" alt="'.$speci.'" title="'.$speci.'" /></li>' ;
}
但它不起作用,然后我意识到图像需要连字符,所以我无论如何不能这样做。关于如何实现这一点,我有什么想法吗?

我会用这个简单的替代品。您还需要将结果放入新变量中,因为您希望同时使用旧值和新值

foreach($specs as $speci){
  $speci_nohyphen = str_replace('-', ' ', $speci);
  echo '<li><img src="', $thepath, '/library/images/icons/species/', $speci, '.gif" width="30" height="30" alt="', $speci_nohyphen, '" title="', $speci_nohyphen, '" /></li>';
}
foreach($specs作为$speci){
$speci_nohyphen=str_replace('-','$speci);
回音“
  • ”; }
    $speci = preg_replace('/-/','\s',$speci);
    
    foreach($specs as $speci){
      $speci_nohyphen = str_replace('-', ' ', $speci);
      echo '<li><img src="', $thepath, '/library/images/icons/species/', $speci, '.gif" width="30" height="30" alt="', $speci_nohyphen, '" title="', $speci_nohyphen, '" /></li>';
    }