使用子字符串从动态变量中删除字符


Using substring to remove characters from a dynamic variable

我正在尝试使用substr从URL字符串中删除4个字符。目标是从字符串中删除.jpg,并将其替换为"-220x124.jpg"。

我使用的是wordpress插件,高级自定义字段,但这不是问题所在。问题是subst无法使用Advanced自定义字段代码_sub_field。它返回整个URL字符串,不删除最后4个字符。知道为什么吗?

以下代码:

<?php if(get_field('still_uploads')): ?>
    <?php $i = 0; ?>
    <?php while(the_repeater_field('still_uploads') && $i <= 0 ): ?>
    <?php 
        $imagejesse =  the_sub_field('still_image');
        $imagejessenew = substr($imagejesse,0,-4); 
    ?>
    <?php echo $imagejessenew.'-220x124.jpg'; ?>
    <?php $i++ ?>
    <?php endwhile; ?>
<?php endif; ?>

您可以在此处看到一个示例:http://gicreative-dev.com/blog/genre/gay/

使用类似的strtr()函数:

$imagejessenew = strtr($imagejesse, array(
    '.jpg' => '-220x124.jpg',
));

请参阅此以获取证据:http://ideone.com/B8ZQe

试试这个:

<?php 
    $imagejessenew = substr(trim(the_sub_field('still_image')),0,-4);
    if($imagejessenew !== FALSE)
    {
        $imagejessenew .= '-220x124.jpg';
    }
    else
    {
        // Shit happened, the_sub_field('still_image') was shorter than 4 characters
    }
?>
$imagejessenew = preg_replace('/(.*)(.(jpg|png|gif))$/i', '$1-220x124.$3', trim($imagejesse));