将图像链接描述中的下划线替换为空格



此代码循环返回任意数量的图像链接。变量$picture->description返回一个包含下划线的文本字符串,这些下划线需要删除。

bluebell_glitter_print_one_piece_with_bubble_skirt_LGG5659WT14_china

我尝试过的所有编码变体都以一个正确的图像链接或一个白色屏幕结束。

如何编辑此循环,使data-caption-desc没有下划线?

if(count($pictures)){               
    foreach($pictures as $picture){
        $output .= '<a href="'.get_option('siteurl').$picture->path.'/'.$picture->filename.'" title="'.$picture->alttext.'" data-caption-title="'.$picture->alttext.'" data-caption-desc="'.$picture->description.'">
                        <img class="wpnggimgcls" src="'.get_option('siteurl').$picture->path.'/thumbs/thumbs_'.$picture->filename.'" style="" />
                    </a>';
    }
}

您应该使用str_replace

str_replace('_', ' ', $input_string);

str_replace将用另一个字符替换一个字符。

完整:

if(count($pictures)) { 
    foreach($pictures as $picture){
        $output .= '<a 
                      href="'.get_option('siteurl').$picture->path.'/'
                      .$picture->filename.'" title="'.$picture->alttext.
                      '" data-caption-title="'.$picture->alttext.'" 
                        data-caption-desc="'
                        .str_replace('_', ' ',$picture->description).'">
                    <img 
                      class="wpnggimgcls" 
                      src="'.get_option('siteurl').
                        $picture->path.'/thumbs/thumbs_'.
                        $picture >filename.'" style="" />
                    </a>';
    }
}

如果只需要将"下划线"替换为空白,则可以使用preg_replace(),如下所示:

<?php
$strChg='bluebell_glitter_print_one_piece_with_bubble_skirt_LGG5659WT14_china';
echo $strChg;
echo '<br />';
echo '<br />';
$Chg=preg_replace('/_/', ' ', $strChg);
echo $Chg;
?>

所以,重点是:

$Chg=preg_replace('/_/', ' ', $strChg);

最新更新