开发者

Using preg replace with an array

开发者 https://www.devze.com 2023-01-23 00:50 出处:网络
I have a checkbox list creating an array that I want to iterate over to have the value create both an image file name as well as the title and alt tag.

I have a checkbox list creating an array that I want to iterate over to have the value create both an image file name as well as the title and alt tag.

$specs = get('speciesCommon');
$thepath = get_bloginfo('template_directory'); 
foreach($specs as $speci){
echo '<li><img src="'.$thepath.'/library/images/开发者_C百科icons/species/'.$speci.'.gif" width="30" height="30" alt="'.$speci.'" title="'.$speci.'" /></li>' ;
}

So for example the checkbox value is grizzly-bear then it would populate this to print:

<img src=".../grizzly-bear.gif" title="grizzly-bear" alt="grizzly-bear" .../>

I want to have just the title and alt tag printed without the hyphen.. I tried..

$speci = preg_replace('/-/','\s',$speci);

but it didnt work and then I realized that the image needs the hyphen so I cant do it that way anyways. Any ideas on how I can make this happen?


I would use str_replace for this simple replacement. You also need to put the result in a new variable since you want to use both the old and the new value.

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>';
}
0

精彩评论

暂无评论...
验证码 换一张
取 消