Using substring to remove characters from a dynamic variable

I am trying to use substr to remove 4 characters from a URL string. The goal is to remove .jpg from the string and replace it with “-220×124.jpg”.

I am using a wordpress plugin, advanced custom fields, but that is not the issue here. The issue is that the subst is not working with the Advanced custom fields code, the_sub_field. It returns the entire URL string without the last 4 characters removed. Any idea why?

Read More

Code below:

<?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; ?>

You can see an example here: http://gicreative-dev.com/blog/genre/gay/

Related posts

Leave a Reply

3 comments

  1. Try this:

    <?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
        }
    ?>